Prompt
I’m building a notification system and need token refresh handling
Latest observation
Nov 10, 2026
OpenAI APIWeb search: off
Here’s a solid pattern for token refresh handling in a notification system.
Core idea
When an access token expires, you should:
- Detect the failure (
401 Unauthorized,invalid_token, etc.) - Refresh the token using a refresh token
- Retry the original request once
- If refresh fails, force re-authentication
Recommended flow
1. Store tokens securely
- Access token: short-lived
- Refresh token: long-lived, stored more securely
- Prefer:
- HTTP-only secure cookies for web apps
- OS secure storage / keychain for mobile
- Encrypted storage for server-side apps
2. Centralize API calls
Wrap all notification API requests in one client so refresh logic is handled in one place.
3. Refresh on 401
Pseudo-flow:
request -> API
-> if success: return data
-> if 401:
if refresh already in progress:
wait for refresh to finish
else:
refresh token
retry request
-> if refresh fails:
log out / re-auth
Example implementation pattern
JavaScript/TypeScript-style pseudo code
let isRefreshing = false;
let refreshPromise: Promise<string> | null = null;
async function apiRequest(url: string, options: RequestInit) {
const accessToken = getAccessToken();
const response = await fetch(url, {
...options,
headers: {
...options.headers,
Authorization: `Bearer ${accessToken}`,
},
});
if (response.status !== 401) {
return response;
}
// Avoid multiple simultaneous refresh calls
if (!isRefreshing) {
isRefreshing = true;
refreshPromise = refreshAccessToken()
.then((newToken) => {
setAccessToken(newToken);
return newToken;
})
.finally(() => {
isRefreshing = false;
refreshPromise = null;
});
}
const newToken = await refreshPromise!;
if (!newToken) throw new Error("Refresh failed");
// Retry original request once
return fetch(url, {
...options,
headers: {
...options.headers,
Authorization: `Bearer ${newToken}`,
},
});
}
Best practices
Avoid infinite retry loops
Only retry once after refresh.
If the retry also returns 401, stop and re-authenticate.
Handle concurrent requests
If 10 requests fail at once, you want one refresh call, not 10.
Detect refresh-token failure explicitly
If refresh returns:
401/403- invalid/expired refresh token
- revoked session
Then clear tokens and redirect/login again.
Add expiry pre-check
If the access token contains an expiry (exp), refresh slightly before it expires to reduce failed requests.
Backoff / rate limiting
If refresh endpoints are overloaded, use:
- exponential backoff
- jitter
- circuit breaker if needed
For notification systems specifically
If your notification system is:
- push-based: ensure the token used to connect to FCM/APNs/WebSocket can be reissued automatically
- polling-based: central API wrapper is enough
- real-time connections: reconnect after refreshing the token
Example for WebSocket-like systems:
- access token expires
- refresh token
- reconnect socket with new access token
Minimal server-side considerations
If your backend sends notifications to third-party APIs:
- store provider credentials securely
- refresh provider OAuth tokens before sending
- if token refresh fails, mark the integration as disconnected and alert the user/admin
If you want, I can also provide:
- a React/Node.js Axios interceptor example
- a mobile (Flutter/React Native) version
- a backend notification-service implementation
- a database schema for access/refresh token storage