Skip to main content

useAuth, useSession, useSignIn

Hooks for the session. Read whether a person is signed in, get an access token for your API, and start sign-in or sign-out from your own UI.

ComponentReadyView source
Works with
ReactNext.js

Installation​

npm install @lumoauth/react

Usage​

Call your API with the access token​

getToken() returns a token that is still valid. It refreshes the token first when it is about to expire.

import { useSession } from '@lumoauth/react';

export function useApi() {
const { getToken } = useSession();

return async (path: string, init: RequestInit = {}) => {
const token = await getToken();
return fetch(`https://api.acme.com${path}`, {
...init,
headers: { ...init.headers, Authorization: `Bearer ${token}` },
});
};
}

Your own sign-in and sign-out controls​

import { useAuth } from '@lumoauth/react';

export function AccountMenu() {
const { isLoaded, isSignedIn, signInWithRedirect, signInWithSocial, signOut } = useAuth();

if (!isLoaded) return null;
if (!isSignedIn) {
return (
<>
<button onClick={() => signInWithRedirect()}>Sign in</button>
<button onClick={() => signInWithSocial('google')}>Continue with Google</button>
</>
);
}
return <button onClick={() => signOut({ afterSignOutUrl: '/' })}>Sign out</button>;
}

useAuth​

Returns the whole auth state and every action.

ReturnsTypeDescription
status'loading' | 'authenticated' | 'unauthenticated'The state of the session.
isLoadedbooleanFalse until the provider has read the stored session.
isSignedInbooleanTrue when a person is signed in.
userLumoAuthUser | nullThe signed-in person. See useUser.
signIn(email?, password?) => Promise<void> | voidPKCE mode: call it with no arguments to go to the hosted sign-in page. Password mode: it calls the OAuth password grant, which LumoAuth does not offer, so it fails.
signInWithRedirect() => voidGo to the hosted sign-in page.
signInWithSocial(provider: string) => voidGo straight to a social provider.
signUp({ email, password, firstName?, lastName? }) => Promise<void>PKCE mode: go to the hosted sign-up page. Password mode: it throws, because LumoAuth has no API that creates an account from a form.
signOut({ afterSignOutUrl? }?) => Promise<void>Revoke the access token, clear the stored tokens and end the session at LumoAuth.
getToken() => Promise<string | null>A valid access token, refreshed first when needed.
handleCallback() => Promise<void>Exchange the code on the callback page. AuthCallback calls it for you.
sendMagicLink(email, returnTo?, { captchaToken? }?) => Promise<void>Ask for a sign-in link by email. See useMagicLink.
checkEmail(email) => Promise<EmailCheckResult>Ask which sign-in methods the organization offers. See useEmailFirst.
authStrategy'pkce' | 'password'The strategy of the provider.
configobjectThe provider settings: domain, orgId, customDomain, clientId, redirectUri and the three after-URLs.

useSession​

ReturnsTypeDescription
isLoadedbooleanFalse until the provider has read the stored session.
isActivebooleanTrue when a person is signed in.
status'loading' | 'authenticated' | 'unauthenticated'The state of the session.
getToken() => Promise<string | null>A valid access token, refreshed first when needed.

useSignIn​

ReturnsTypeDescription
signIn(email?, password?) => Promise<void> | voidThe same function as in useAuth.
signInWithRedirect() => voidGo to the hosted sign-in page.
isLoadingbooleanTrue while the session loads.