Skip to content

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).

  1. 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.
  2. 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.services and your-app-dev-spa.
  3. 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.
Terminal window
npm install @seek-services/auth-react

It brings auth-js, the browser core, with it, and needs React 18 or 19. See Installing.

src/main.tsx
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.

src/App.tsx
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.

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.1
Host: api.your-app.com
Authorization: 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.

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.

The production environment has its own users, settings and issuer (https://your-app.auth.dev.seek.services), and no client until you create one:

  1. 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.
  2. Point issuer and clientId at the production values, for example from your build’s environment variables.
  3. Bring your own credentials for the social providers you enable; development’s shared ones do not work there.

See Going to production.