▸ 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
▸ 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