Keep users signed in
The access token lives 5 minutes; the refresh token gets new ones for 30 days and changes at every use. This page shows a small session module that handles both correctly.
The session module
import { refreshTokens, revokeTokens, type Tokens } from '@novren/id-client/app'
const ID_SERVER = 'https://id.novren.net'
const CLIENT_ID = 'cl-0123456789abcdef0123'
let tokens: Tokens | null = null
let expiresAt = 0
let refreshing: Promise<Tokens | null> | null = null
export function startSession(fresh: Tokens) {
tokens = fresh
expiresAt = Date.now() + fresh.expires_in * 1000
}
/** A valid access token, refreshed 30 seconds before it expires. `null`: sign in again. */
export async function accessToken(): Promise<string | null> {
if (!tokens) return null
if (Date.now() < expiresAt - 30_000) return tokens.access_token
// Only ONE refresh at a time: the refresh token changes on every use, and presenting
// an old one twice closes the whole session (reuse detection).
refreshing ??= refreshTokens({ idServerUrl: ID_SERVER, clientId: CLIENT_ID, refreshToken: tokens.refresh_token }).finally(() => {
refreshing = null
})
const next = await refreshing
if (!next) {
tokens = null // session over: app disconnected, password changed, token reused…
return null
}
startSession(next) // ALWAYS keep the new refresh token
return next.access_token
}
export async function signOut() {
if (tokens) await revokeTokens({ idServerUrl: ID_SERVER, clientId: CLIENT_ID, refreshToken: tokens.refresh_token })
tokens = null
}And to call your API:
export async function callApi(path: string, init: RequestInit = {}) {
const token = await accessToken()
if (!token) throw new Error('signed out')
const headers = new Headers(init.headers)
headers.set('authorization', `Bearer ${token}`)
return fetch(path, { ...init, headers })
}The three rules
- Always store the new refresh token. The answer to a refresh contains a new
refresh_token; the old one is dead. - One refresh at a time. Two tabs or two requests that refresh with the same token at the same moment look like a reuse: Novren ID closes the session. In the module above, concurrent calls share the same promise. Across tabs, use a
BroadcastChannelor the Web Locks API (navigator.locks.request('novren-refresh', …)). nullmeans sign in again.refreshTokensreturnsnullwhen Novren ID answers 400 or 401: the chain is closed. Any other failure (network, 5xx) throws: don't sign the user out for that, retry later.
Where to keep the tokens
| Where | Survives a reload | Risk |
|---|---|---|
| Memory (a variable) | no | lowest: gone with the tab |
sessionStorage | yes, same tab | readable by any script on your origin |
localStorage | yes, all tabs | readable by any script on your origin, for longer |
There is no perfect answer in the browser. Many apps keep the refresh token in memory and, after a reload, run the sign-in again: if the user still has a session on account.novren.net, it is a quick round trip with the consent page.
productKey is a CryptoKey: it can be stored in IndexedDB and stays non-extractable there. Whether to do it is your call: it saves a sign-in after a reload, but the key then lives on the disk of that browser profile.
Logout
revokeTokens closes the chain on Novren ID: the refresh token dies at once, and your server refuses the chain's access tokens within seconds (through the revocation feed). It never throws: logout must work even offline. Then clear what your app keeps (tokens, productKey, IndexedDB).
Logging out of your app does not sign the user out of account.novren.net or other apps. That's the expected behaviour of shared identity (like signing out of one site that uses "Sign in with Google").
When the session ends by itself
Your app finds out at the next refresh (null) or when your server answers 401. The reasons are in Tokens and sessions: password change, the user disconnected your app, you disabled the client, reuse, 30 days without use.