Using a Backend for Frontend (BFF)
A Backend for Frontend (BFF) is a small server that belongs to your app. It signs users in and keeps the tokens, so the refresh token never reaches the browser, and it receives back-channel logout. Use this setup when you want to keep your React app but need single logout or safer token storage.
About 15 minutes.
Compared with React only
| | React only | React + BFF |
|---|---|---|
| Where tokens live | Browser sessionStorage | The BFF. The browser gets an access token only if the app asks for one |
| Single logout | Not supported | Supported |
| Client type | Public, no secret | Confidential, with a client secret |
Set up the BFF
1. Register a confidential application
In the Console, create an application with a client secret. Open it, go to Advanced Settings → OAuth2 Configuration, and set these URIs. The redirect URIs use the React app's origin. The back-channel logout URI points at the BFF itself, because Choreo Identity & Access Management calls it directly, server to server.
Authorized redirect URI http://localhost:5173/api/auth/callback
Post-logout redirect URI http://localhost:5173
Back-channel logout URI http://localhost:3001/api/auth/backchannel-logout
2. Install the Express SDK
npm
npm install express cookie-parser @thunderid/express
Yarn
yarn add express cookie-parser @thunderid/express
pnpm
pnpm add express cookie-parser @thunderid/express
Bun
bun add express cookie-parser @thunderid/express
3. Add the environment variables
Keep the client secret on the server. APP_URL is the origin the browser opens the React app on.
THUNDERID_BASE_URL=https://<DATA_PLANE_URL>
THUNDERID_CLIENT_ID=<your-client-id>
THUNDERID_CLIENT_SECRET=<your-client-secret>
APP_URL=http://localhost:5173
4. Mount the SDK and receive back-channel logout
Mount thunderID() under /api/auth and add the back-channel logout route. The SDK stores the tokens on the server and sets the HttpOnly session cookie. Start the server with node --env-file=.env index.mjs.
import express from "express"
import cookieParser from "cookie-parser"
import { thunderID, handleBackchannelLogout } from '@thunderid/express'
const appUrl = process.env.APP_URL
const app = express()
app.use(cookieParser())
const auth = express.Router()
app.use('/api/auth', auth)
auth.use(
thunderID({
baseUrl: process.env.THUNDERID_BASE_URL,
clientId: process.env.THUNDERID_CLIENT_ID,
clientSecret: process.env.THUNDERID_CLIENT_SECRET,
afterSignInUrl: `${appUrl}/api/auth/callback`,
afterSignOutUrl: appUrl,
sessionCookie: { httpOnly: true, sameSite: 'lax' },
}),
)
// Choreo Identity & Access Management posts a logout token here when the user's session ends elsewhere.
auth.post('/backchannel-logout', handleBackchannelLogout())
// Add the endpoints the React SDK calls. See "Endpoints the React SDK calls".
app.listen(3001)
Connect the React app
1. Serve the BFF on the app's origin
Forward /api/auth to the BFF, so the session cookie is first party and you need no CORS setup. In production, serve the React build and the BFF from the same origin, for example behind one reverse proxy.
import { defineConfig } from 'vite'
import react from "@vitejs/plugin-react"
export default defineConfig({
plugins: [react()],
server: {
proxy: {
'/api/auth': { target: 'http://localhost:3001' },
},
},
})
2. Switch the provider to BFF mode
Set endpoints.backend. The app needs no client ID or base URL, because every endpoint defaults to a path under /api/auth on the app's origin. session.checkInterval sets how often, in seconds, the SDK checks that the session is still live.
<ThunderIDProvider
endpoints={{ backend: {} }}
session={{ checkInterval: 30 }}
>
<App />
</ThunderIDProvider>
Hooks and components work as before. Calls through the SDK's http client go to /api/auth/proxy/*, and the BFF adds the access token.
Endpoints the React SDK calls
The BFF serves these under /api/auth. The SDK sends the header X-Requested-With: XMLHttpRequest on every request it makes with fetch. Reject requests without it, except the sign-in redirects, so a page on another site cannot use the session cookie.
| Endpoint | What it does |
|---|---|
| GET /signin | Starts sign-in. The browser navigates here |
| GET /callback | The redirect URI. Exchanges the code and sets the session cookie |
| GET /session | Returns the signed-in user |
| GET /checksession | Tells the app whether the session is still live. Must not refresh the session |
| POST /refresh | Refreshes the access token on the server |
| POST /signout | Ends the session and returns the end-session URL |
| POST /token | Returns an access token for APIs the app calls directly. Leave it out if the app sends every call through the proxy |
| ALL /proxy/* | Forwards API calls with the access token |
Try single logout
1. Allow a local back-channel logout URI
Choreo Identity & Access Management rejects localhost and private network addresses as back-channel logout URIs by default. For local testing only, allow them in deployment.yaml and restart the server.
oauth:
logout:
backchannel:
reject_private_addresses: false
2. Add single sign-on to the sign-in flow
The flow needs the Check SSO Session and Save / Load Session nodes. Without them Choreo Identity & Access Management records no session, so it sends no logout.
3. Sign out from another app
Sign in to this app and to a second app that uses the same Choreo Identity & Access Management, in the same browser. Sign out of the second app. Within 30 seconds, or as soon as you switch back to this app's tab, it signs you out too.
Beyond this guide
Was this page helpful?