▸ UserContextProvider(__namedParameters): JSX.Element
This library implements the Authorization Code Grant Flow with PKCE.
import { UserContextProvider } from '@emdgroup/react-auth';
function App(): JSX.Element {
return (
<UserContextProvider autoLogin clientId="...">
<Content />
</UserContextProvider>
);
}
function Content(): JSX.Element {
const { info } = useUser();
return <p>{info.email}</p>;
}| Name | Type |
|---|---|
__namedParameters |
ProviderOptions |
JSX.Element
▸ getAccessToken(__namedParameters): Promise<string>
Return a valid access token for the current user. If the stored session is expired and a refresh token is available, the function attempts to refresh the session automatically. If the user is not authenticated, it rejects with an error. On refresh failure, the stored session is cleared.
const token = await getAccessToken({
idpHost: 'login.emddigital.com',
clientId: 'my-client-id',
});
fetch('/api/resource', { headers: { Authorization: `Bearer ${token}` } });Throws
Error when the user is not authenticated (no stored session).
| Name | Type |
|---|---|
__namedParameters |
Object |
__namedParameters.clientId |
string |
__namedParameters.idpHost |
string |
__namedParameters.signal? |
AbortSignal |
Promise<string>
Promise that resolves to a bearer access token string.
▸ refreshSession(__namedParameters): Promise<UserSession>
Exchange a refresh token for a new session using the OAuth 2.0 refresh_token grant.
This function deduplicates concurrent calls: multiple invocations while a refresh is in progress will share the same promise. On success, it updates localStorage with the new session and emits a storage event to synchronize other tabs/components.
const session = await refreshSession({
idpHost: 'login.emddigital.com',
clientId: 'my-client-id',
refreshToken,
});
// session.accessToken, session.expires, ...| Name | Type |
|---|---|
__namedParameters |
Object |
__namedParameters.clientId |
string |
__namedParameters.idpHost |
string |
__namedParameters.refreshToken |
string |
__namedParameters.signal? |
AbortSignal |
Promise<UserSession>
Promise that resolves to the renewed UserSession.
▸ useUser(): UserContext
Returns the user context previously established with UserContextProvider.
const { info, session, login, logout, authHeader } = useUser();
useEffect(() => {
if (!session) return; // user is not logged in
fetch('/api/pet', { headers: authHeader }).then(...);
}, [session, authHeader]);UserContext
The user context object is returned by the useUser hook.
• Optional authHeader: Object
Convenience header object containing the Authorization header value set to the access token.
▪ [key: string]: string
• Optional info: UserInfo
Provides the UserInfo object if the user is authenticated.
• Optional login: (opts?: LoginOptions) => void
▸ (opts?): void
Function to initiate the login flow.
| Name | Type |
|---|---|
opts? |
LoginOptions |
void
• Optional loginUrl: string
Set to the URL that the user is redirected to initiate the authorization flow. Useful when you need to start the login flow in a separate window or tab. Use in combination with login({ refresh: false }).
• Optional logout: () => void
▸ (): void
Function to log the user out.
void
• Optional session: UserSession
Provides the UserSession object if the user is authenticated.
• Optional acrValues: string
Request a type of multi-factor authentication. Currently, mfa is the only supported value.
• Optional additionalParameters: string
Additional query parameters, such as state=xyz.
• Optional autoLogin: boolean
When enabled, the user will automatically be logged in when the page is loaded. Defaults to false.
• clientId: string
Client ID as provided by the IdP.
• Optional domainHint: string
Domain name to directly forward a user to the login page for a certain auth domain.
• Optional idpHost: string
Overwrite the IdP host, defaults to login.emddigital.com.
• Optional prompt: "login"
Whether the authorization server prompts the user for re-authentication.
• Optional redirectUri: string
Overwrite the redirect URI, defaults to the current hostname + /auth.
• Optional refreshSession: boolean
Persist and use the refreshToken to renew an expired accessToken. Defaults to false.
• Optional userInfoEndpoint: string
Overwrite the userinfo endpoint, defaults to /oauth2/userinfo.
Object representing the options for the login function of the UserContext.
• Optional entrypoint: string
Entrypoint to redirect the user to after successful authentication. Defaults to the URL that the user initially visited.
• Optional redirect: boolean
Automatically redirect the user to the login URL and to the entrypoint after successful authentication. Disabling this will disable all redirects. Defaults to false.
Object containing the OIDC tokens and expiration time.
• accessToken: string
OAuth access token provided by the IDP
• expires: number
Epoch time in seconds when the access token expires
• Optional idToken: string
OAuth ID token provided by the IDP
• Optional refreshToken: string
OAuth refresh token provided by the IDP
Object representing the user details as provided by the IdP userInfo endpoint.
-
Record<string,unknown>↳
UserInfo
• email: string
Email address
• Optional familyName: string
Family name of provided
• Optional givenName: string
Given name of provided
• sub: string
Subject identifier