React single-page app
In this quickstart a React app gets a login that opens on its own page, shows who is signed in, and calls an API of yours as that user. The tokens stay in the browser, bound to a key that cannot leave it (DPoP).
Before you start
Section titled “Before you start”- Create an app in the dashboard. It gets a development and a production environment, and the development one already has a client for a single-page app:
{slug}-dev-spa. - Open the app’s Quickstart page. It shows the development issuer and client id. In this guide they are
https://your-app-dev.auth.dev.seek.servicesandyour-app-dev-spa. - Development accepts any page on
http://localhost:<port>with no further setup. Email codes are on from the start; turn other methods on in Login methods.
1. Install the SDK
Section titled “1. Install the SDK”npm install @seek-services/auth-reactIt brings auth-js, the browser core, with it, and needs React 18 or 19. See Installing.
2. Wrap your app in the provider
Section titled “2. Wrap your app in the provider”import { StrictMode } from 'react'import { createRoot } from 'react-dom/client'import { AuthProvider } from '@seek-services/auth-react'import { App } from './App'
createRoot(document.getElementById('root')!).render( <StrictMode> <AuthProvider issuer='https://your-app-dev.auth.dev.seek.services' clientId='your-app-dev-spa'> <App /> </AuthProvider> </StrictMode>,)<AuthProvider> adds the login’s stylesheet to the page, renders the login dialog, and on every page load picks the stored session up again.
3. Log in and out
Section titled “3. Log in and out”import { useState } from 'react'import { authFetch, useAuthStore } from '@seek-services/auth-react'
export function App() { const { user, status, login, logout, retry } = useAuthStore() const [orders, setOrders] = useState<unknown>(null)
if (status === 'loading') return null if (status === 'unavailable') return <button onClick={retry}>Can't reach the server. Try again</button> if (!user) return <button onClick={login}>Log in</button>
const loadOrders = async () => { const res = await authFetch('https://api.your-app.com/orders', { resource: 'orders-api' }) setOrders(await res.json()) }
return ( <main> <p>Signed in as {user.email}</p> <button onClick={loadOrders}>Load orders</button> <button onClick={logout}>Log out</button> <pre>{JSON.stringify(orders, null, 2)}</pre> </main> )}Start your dev server and press Log in: the dialog asks for an email address and sends a six-digit code. An address that has no account yet signs up with the same code. user then holds who is signed in (id, email, name, acr, amr, …), and the session survives reloads until the user logs out or it ends.
4. Call your API
Section titled “4. Call your API”An access token is for one API. Create yours in APIs and roles: an audience such as orders-api and permissions such as orders:read. Then name it with resource when you call it, as loadOrders does above. authFetch adds the token and a fresh DPoP proof to the request:
GET /orders HTTP/1.1Host: api.your-app.comAuthorization: DPoP eyJhbGciOiJFUzI1NiIsInR5cCI6ImF0K2p3dCIs...DPoP: eyJhbGciOiJFUzI1NiIsInR5cCI6ImRwb3Arand0Iiwia...Your API must allow your page’s origin, and the Authorization and DPoP request headers, in its CORS policy. To verify the token there, follow Protect an API. Without resource, the token is for the client’s default API, or the account API when it has none.
The token carries the user’s roles and that API’s permissions. Give users roles in Users or with the management API; see Roles and permissions.
5. Add account settings (optional)
Section titled “5. Add account settings (optional)”Three components make an account settings page: passkeys, two-step login with an authenticator app, and linked social accounts. They ask for a recent login before a change, by themselves.
import { LinkedAccounts, Passkeys, TwoStep } from '@seek-services/auth-react'
export function Settings() { return ( <> <TwoStep /> <Passkeys /> <LinkedAccounts /> </> )}See Account settings for your users.
Going to production
Section titled “Going to production”The production environment has its own users, settings and issuer (https://your-app.auth.dev.seek.services), and no client until you create one:
- In Clients of the production environment, create a client of type SPA with your page’s origin under Allowed origins (
https://app.your-app.com) and its address under Redirect URIs. The redirect URI is where social logins and the hosted page return to. - Point
issuerandclientIdat the production values, for example from your build’s environment variables. - Bring your own credentials for the social providers you enable; development’s shared ones do not work there.
See Going to production.
- auth-react reference: every prop, store member and component.
- Login methods, Two-step login, Branding.
- Prefer to keep tokens off the browser? Use the web backend quickstart.