This HTML page is not optimized for LLM or AI agent consumption. Fetch the Markdown version instead: /guides/web/viewer/client-authentication.md — it contains the complete documentation content in clean, structured Markdown without any CSS, JavaScript, or navigation noise. Client authentication and JWT session renewal | Nutrient

When you use Nutrient Web SDK with Document Engine in server-backed mode, a JSON Web Token (JWT) authenticates the viewer. For short-lived JWTs, you can refresh authentication at runtime without unloading and reloading the viewer.

Runtime refresh also works for DWS-managed documents. Pass a DWS Viewer API session token as session and add onAuthFailed to the same NutrientViewer.load() call. Then call setSession() with each new session token. With DWS, each refresh counts as another viewer session.

Runtime JWT refresh API contract

  • NutrientViewer.Configuration#onAuthFailed has the signature () => void | Promise<void>.
    • It receives no arguments.
    • Inside this callback, fetch a fresh JWT and call instance.setSession(newJwt).
  • NutrientViewer.Instance#setSession has the signature setSession(session: string): void.
    • Pass a raw JWT string.
    • Don’t pass { jwt: ... } or an authPayload object.
    • In Web SDK 1.23 and later, the new JWT can represent a different collaboration context, not only a refreshed token for the same context.

Reactive refresh: onAuthFailed + setSession

Use this pattern when a request fails due to expired or invalid authentication:

let instance;
instance = await NutrientViewer.load({
container: "#viewer",
serverUrl: "https://your-document-engine.example.com",
documentId: "your-document-id",
authPayload: { jwt: initialJwt },
onAuthFailed: async () => {
const newJwt = await fetchNewJwtFromBackend();
if (!instance) {
throw new Error("Viewer instance is not available yet.");
}
instance.setSession(newJwt);
},
});

onAuthFailed is configured at load() time and doesn’t receive the instance. Use a closure or mutable reference (as shown above) so the callback can call setSession on the current instance.

If setSession() isn’t called within 30 seconds while handling an authentication failure, the refresh times out and the affected requests fail.

Proactive refresh: Call setSession before expiry

If you know the JWT expiry time in advance, refresh before the token expires:

function parseJwtExp(jwt) {
const [, payload] = jwt.split(".");
return JSON.parse(atob(payload)).exp;
}
function scheduleJwtRefresh(instance, jwt) {
const exp = parseJwtExp(jwt);
const refreshBuffer = 5 * 60 * 1000; // Refresh 5 minutes early.
const timeout = Math.max(exp * 1000 - Date.now() - refreshBuffer, 0);
setTimeout(async () => {
const newJwt = await fetchNewJwtFromBackend();
instance.setSession(newJwt);
// Schedule the next proactive refresh.
scheduleJwtRefresh(instance, newJwt);
}, timeout);
}

This keeps the same viewer instance alive while rotating session tokens.

Expected behavior during refresh

  • If the token refresh succeeds, the current viewer session stays active and document operations continue without reloading the instance.
  • If the new JWT differs from the current one only in exp, nbf, iat, or jti, pending changes stay queued and resume syncing under the new token.
  • If the new JWT changes the collaboration context, such as user_id, collaboration_permissions, or default_group, the viewer discards unsynced changes. It reloads the records the new JWT allows in the background, and setSession() doesn’t wait for that reload.
  • This context-switching behavior isn’t supported for app-provided documents loaded with DWS Viewer API.
  • If the refresh fails or times out, authenticated requests fail, and the user may need to retry after your app obtains a fresh JWT.

Mode limitations

  • This flow is supported only in server-backed mode.
  • It isn’t supported when loading app-provided documents with DWS Viewer API. In that flow, DWS Viewer API checks the session token only when the viewer loads. An open viewer keeps working after the token expires. For details, refer to the offline use and token lifetime section in the open app-provided documents guide.
  • It isn’t supported when useDeprecatedRestProvider is enabled.
  • Calling setSession() in standalone mode is unsupported and only logs a warning.