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.
Installation
- npm
- pnpm
- Yarn
- Bun
npm install @lumoauth/react
pnpm add @lumoauth/react
yarn add @lumoauth/react
bun add @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.
| Returns | Type | Description |
|---|---|---|
status | 'loading' | 'authenticated' | 'unauthenticated' | The state of the session. |
isLoaded | boolean | False until the provider has read the stored session. |
isSignedIn | boolean | True when a person is signed in. |
user | LumoAuthUser | null | The signed-in person. See useUser. |
signIn | (email?, password?) => Promise<void> | void | PKCE 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 | () => void | Go to the hosted sign-in page. |
signInWithSocial | (provider: string) => void | Go 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. |
config | object | The provider settings: domain, orgId, customDomain, clientId, redirectUri and the three after-URLs. |
useSession
| Returns | Type | Description |
|---|---|---|
isLoaded | boolean | False until the provider has read the stored session. |
isActive | boolean | True 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
| Returns | Type | Description |
|---|---|---|
signIn | (email?, password?) => Promise<void> | void | The same function as in useAuth. |
signInWithRedirect | () => void | Go to the hosted sign-in page. |
isLoading | boolean | True while the session loads. |