LeoYeAI/agent-master-skills / skills/azure-auth
azure-auth
VerifiedMicrosoft Entra ID (Azure AD) authentication for React SPAs with MSAL.js and Cloudflare Workers JWT validation using jose library. Full-stack pattern with Authorization Code Flow + PKCE. Prevents 8 documented errors. Use when: implementing Microsoft SSO, troubleshooting AADSTS50058 loops, AADSTS700084 refresh token errors, React Router redirects, setActiveAccount re-render issues, or validating Entra ID tokens in Workers.
| File | Kind | Size |
|---|---|---|
| .claude-plugin/plugin.json | config | 688 B |
| README.md | markdown | 2.5 KB |
| SKILL.md | skill md | 19.7 KB |
| _meta.json | config | 284 B |
| references/aadsts-error-codes.md | markdown | 7.7 KB |
| rules/azure-auth.md | markdown | 3.4 KB |
| templates/msal-config.ts | script | 2.9 KB |
| templates/msal-provider.tsx | other | 4.5 KB |
| templates/protected-route.tsx | other | 7.1 KB |
| templates/use-api-token.ts | script | 6.0 KB |
| templates/workers-jwt-validation.ts | script | 8.3 KB |
templates/use-api-token.ts
View on GitHub/**
* React Hook for Acquiring API Tokens
*
* Provides a hook to safely acquire access tokens for calling your backend API.
* Handles silent token acquisition with automatic fallback to interactive login
* when tokens expire or need consent.
*/
import { useMsal } from "@azure/msal-react";
import {
InteractionRequiredAuthError,
SilentRequest,
} from "@azure/msal-browser";
import { useCallback, useState } from "react";
import { apiRequest } from "./msal-config";
interface UseApiTokenResult {
/** Get an access token for API calls */
getAccessToken: () => Promise<string | null>;
/** Whether a token acquisition is in progress */
isLoading: boolean;
/** Any error that occurred during token acquisition */
error: Error | null;
/** Clear any stored error */
clearError: () => void;
}
/**
* Hook for acquiring access tokens for API calls.
*
* Usage:
* ```tsx
* function ApiComponent() {
* const { getAccessToken, isLoading, error } = useApiToken();
*
* async function fetchData() {
* const token = await getAccessToken();
* if (!token) return;
*
* const response = await fetch("/api/data", {
* headers: {
* Authorization: `Bearer ${token}`,
* },
* });
* // handle response...
* }
*
* return (
* <button onClick={fetchData} disabled={isLoading}>
* {isLoading ? "Loading..." : "Fetch Data"}
* </button>
* );
* }
* ```
*/
export function useApiToken(): UseApiTokenResult {
const { instance, accounts } = useMsal();
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState<Error | null>(null);
const getAccessToken = useCallback(async (): Promise<string | null> => {
// Check if user is signed in
if (accounts.length === 0) {
setError(new Error("No user signed in"));
return null;
}
setIsLoading(true);
setError(null);
const request: SilentRequest = {
...apiRequest,
account: accounts[0],
};
try {
// Try silent token acquisition first
// This uses cached tokens or refresh tokens
const response = await instance.acquireTokenSilent(request);
return response.accessToken;
} catch (err) {
// Handle interaction required errors
// These occur when:
// - Refresh token expired (AADSTS700084 - 24hr limit for SPAs)
// - Consent needed for new scopes
// - MFA required
// - Password changed
if (err instanceof InteractionRequiredAuthError) {
try {
// Fall back to interactive login
// Using redirect instead of popup for better mobile support
await instance.acquireTokenRedirect(request);
// This will redirect, so we won't reach here
return null;
} catch (redirectError) {
const error = new Error(
`Interactive login failed: ${redirectError}`
);
setError(error);
return null;
}
}
// Other errors (network, configuration, etc.)
const error =
err instanceof Error ? err : new Error("Failed to acquire token");
setError(error);
return null;
} finally {
setIsLoading(false);
}
}, [instance, accounts]);
const clearError = useCallback(() => {
setError(null);
}, []);
return {
getAccessToken,
isLoading,
error,
clearError,
};
}
/**
* Higher-order function to create an authenticated fetch wrapper.
*
* Usage:
* ```tsx
* function MyComponent() {
* const { getAccessToken } = useApiToken();
* const authFetch = useAuthenticatedFetch(getAccessToken);
*
* async function loadData() {
* const response = await authFetch("/api/data");
* const data = await response.json();
* }
* }
* ```
*/
export function useAuthenticatedFetch(
getAccessToken: () => Promise<string | null>
) {
return useCallback(
async (url: string, options: RequestInit = {}): Promise<Response> => {
const token = await getAccessToken();
if (!token) {
throw new Error("Failed to acquire access token");
}
const headers = new Headers(options.headers);
headers.set("Authorization", `Bearer ${token}`);
return fetch(url, {
...options,
headers,
});
},
[getAccessToken]
);
}
/**
* Hook for making authenticated API calls with automatic token handling.
*
* Usage:
* ```tsx
* function DataComponent() {
* const { data, isLoading, error, refetch } = useAuthenticatedQuery<User[]>(
* "/api/users"
* );
*
* if (isLoading) return <Loading />;
* if (error) return <Error message={error.message} />;
*
* return <UserList users={data} />;
* }
* ```
*/
export function useAuthenticatedQuery<T>(
url: string,
options?: RequestInit
): {
data: T | null;
isLoading: boolean;
error: Error | null;
refetch: () => void;
} {
const { getAccessToken } = useApiToken();
const [data, setData] = useState<T | null>(null);
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState<Error | null>(null);
const [fetchTrigger, setFetchTrigger] = useState(0);
const refetch = useCallback(() => {
setFetchTrigger((prev) => prev + 1);
}, []);
// Fetch data when component mounts or refetch is called
useCallback(async () => {
setIsLoading(true);
setError(null);
try {
const token = await getAccessToken();
if (!token) {
throw new Error("Failed to acquire access token");
}
const headers = new Headers(options?.headers);
headers.set("Authorization", `Bearer ${token}`);
const response = await fetch(url, {
...options,
headers,
});
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
}
const result = (await response.json()) as T;
setData(result);
} catch (err) {
setError(err instanceof Error ? err : new Error("Failed to fetch data"));
} finally {
setIsLoading(false);
}
}, [url, options, getAccessToken, fetchTrigger]);
return { data, isLoading, error, refetch };
}
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| writing-skillsobra/superpowersUse when creating new skills, editing existing skills, or verifying skills work before deployment | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| xlsxanthropics/skillsUse this skill any time a spreadsheet file is the primary input or output. This means any task where the user wants to: open, read, edit, or fix an existing .xlsx, .xlsm, .xltx, .csv, or .tsv file (e.g., adding columns, computing formulas, formatting, charting, cleaning messy data); create a new spreadsheet from scratch or from other data sources; or convert between tabular file formats. Trigger especially when the user references a spreadsheet file by name or path — even casually (like "the xlsx in my downloads") — and wants something done to it or produced from it. Also trigger for cleaning or restructuring messy tabular data files (malformed rows, misplaced headers, junk data) into proper spreadsheets. The deliverable must be a spreadsheet file. Do NOT trigger when the primary deliverable is a Word document, HTML report, standalone Python script, database pipeline, or Google Sheets API integration, even if tabular data is involved. | Stars: 198k | Updated | Claude CodeCodex | Verified | Install |
| shipping-and-launchaddyosmani/agent-skillsPrepares production launches. Use when preparing to deploy to production, or when asking what needs to be in place before shipping. Use when you need a pre-launch checklist, when setting up monitoring, when planning a staged rollout, or when you need a rollback strategy. | Stars: 119k | Updated | Claude CodeCodex | Verified | Install |
| childhoodthedotmack/claude-memBabysit a shipped release after merge, publication, or deployment. Verify published packages, production behavior, installed plugins, and late CI or user reports until the observation window is clean; use babysit for PR readiness before release. | Stars: 118k | Updated | Claude CodeCodex | Verified | Install |