Identity
Render per-user private islands with signed tokens, from host setup to key rotation.
Render an island for the signed-in user. The host signs a short-lived token, the remote verifies it, and the island reads the user with getIdentity(). For claims and sign-out behaviour, see Identity reference.
Good to know: Examples: examples/next-app-router with examples/vite-remote, and examples/advanced for several remotes.
Set up the host (App Router)
Add identity to the federation config
Add identity to the federation config. Read the secrets from the environment.
import { defineFederation } from '@next-federation/core'
export const federation = defineFederation({
remotes: {
shop: { origin: process.env.SHOP_ORIGIN ?? 'https://shop.example.com' },
},
identity: {
// Must equal the key in the remote's `issuers` map.
issuer: process.env.NF_ISSUER ?? 'https://www.example.com',
signingKeys: process.env.NF_SIGNING_KEY!,
},
})Add getIdentity and the routes
getIdentity returns { sub, claims? } for a signed-in user, or null. Return null instead of throwing.
import 'server-only'
import { createFederationRoutes } from '@next-federation/next/routes'
import { createIslands } from '@next-federation/next/server'
import type { AppIdentity } from '@next-federation/next/server'
import { federation } from './federation.config'
export { federation }
const getIdentity: AppIdentity = ({ cookies }) => {
const sub = cookies.get('session-user')?.value
return sub ? { sub } : null
}
export const { RemoteIsland } = createIslands(federation, { getIdentity })
export const routes = createFederationRoutes(federation, { getIdentity })Mount the token routes
Create this file. It serves GET <routesPath>/jwks and POST <routesPath>/token?aud=<remote>. routesPath defaults to /api/next-federation.
import { routes } from '../../../../lib/federation'
export const { GET, POST } = routesGenerate a signing key
Run this script once with node scripts/generate-key.mjs.
import { generateSigningKey } from '@next-federation/core/identity'
console.log(await generateSigningKey('2026-11'))It prints a private JWK. Put it in .env.local (git-ignored) and in your production secret store.
NF_SIGNING_KEY='{"kty":"EC","x":"...","y":"...","crv":"P-256","d":"...","kid":"2026-11","alg":"ES256"}'
NF_ISSUER='http://localhost:3000'To fail at boot on a malformed key, call validateIdentity(federation) from instrumentation.ts.
export async function register() {
if (process.env.NEXT_RUNTIME !== 'nodejs') return
const { validateIdentity } = await import('@next-federation/core/identity')
const { federation } = await import('./lib/federation.config')
await validateIdentity(federation)
}Mark the island private on the remote
List the host's issuer and JWKS URL in issuers, and mark the island private: true.
issuers: { 'https://www.example.com': 'https://www.example.com/api/next-federation/jwks' },
islands: { MyOrders: { entry: './src/islands/MyOrders.tsx', private: true } },The issuer must equal the host's identity.issuer exactly, and hosts holds the host's origin. A new host origin needs a rebuild. See Issuers and JWKS URLs.
Render the island for the user
Mark the island private and give it an errorFallback. A visitor who is not signed in sees it.
<RemoteIsland
remote="shop"
island="MyOrders"
private
fallback={<p>loading orders…</p>}
errorFallback={<p>sign in to see your orders</p>}
/>In the island, getIdentity() from @next-federation/island returns the verified payload: sub, your claims, iss, aud, iat and exp.
Good to know: sub is unique per issuer only. Key data by (iss, sub)
when a remote trusts several hosts. See What getIdentity
returns.
Sign users out
On sign-out, clear the cookie, call clearIslandTokens() from @next-federation/react, then refresh the route.
'use client'
import { clearIslandTokens } from '@next-federation/react'
import { useRouter } from 'next/navigation'
export function SignOutButton() {
const router = useRouter()
return (
<button
onClick={async () => {
await fetch('/api/logout', { method: 'POST' }) // with a basePath: `${basePath}/api/logout`
clearIslandTokens()
router.refresh()
}}
>
Sign out
</button>
)
}See Sign in and sign out for demo routes and what happens to issued tokens.
Set up the host (Pages Router)
getIdentity takes the request. Private islands work in getServerSideProps only. In getStaticProps they throw NF-002.
import { createPagesIslands } from '@next-federation/next/pages/server'
import type { PagesIdentity } from '@next-federation/next/pages/server'
import { federation } from './federation.config'
export { federation }
export const getIdentity: PagesIdentity = (req) => {
const sub = req.cookies['session-user']
return sub ? { sub } : null
}
export const { getIslandProps } = createPagesIslands(federation, {
getIdentity,
})import { createFederationApiHandler } from '@next-federation/next/pages/server'
import { federation, getIdentity } from '../../../lib/federation'
export default createFederationApiHandler(federation, { getIdentity })Sign-in and sign-out are API routes that set and clear the same cookie. Call clearIslandTokens() on sign-out.
Rotate a signing key
Remotes cache the JWKS for up to 10 minutes. To rotate without rejected tokens:
Publish the new key
Add the new key to identity.signingKeys as a non-first entry. It is published but does not sign.
Wait for the caches
Wait 10 minutes.
Start signing with it
Move the new key to the first position. It now signs.
Retire the old key
Keep the old key published until its last token expires, then remove it.
The per-user marker changes for every user at step 3, so anything cached per marker refreshes once.
Next steps
Identity reference
The token model, claims, issuers, sign-out behaviour and what to expect.
Server Actions in islands
Act as the signed-in user from an island, and refuse unauthenticated actions.
Writing an island remote
Configure hosts and issuers on the remote.
Caching
Private islands and the first HTML.