How-to guide·AuthenticationJavaScript · Java · .NET

Refresh an expired token

Keep a browser page working past the lifetime of its delegated token with the JavaScript SDK's expired-token handler, and what the other SDKs do instead.

For a web page that runs on a short-lived delegated token (versions, nodes or recursive-node token, or an anonymous login) and may stay open longer than that token lives. The JavaScript SDK can fetch a new auth token the moment it needs one, through a handler you write; the handler's job is to ask your backend for a fresh token.

Before you start#

1. Register the handler#

The handler receives the expired auth object and must return a promise of a new one. The client stores the new object and uses it from then on.

JavaScript#
const client = await factory.createClientWithAuth(auth);

client.registerExpiredTokenHandler(async (expiredAuth) => {
	// Your backend validates the user's own session (a cookie, a header) and issues a new token
	const response = await fetch("/api/bimworks-token", {credentials: "include"});
	if (!response.ok) {
		throw new Error("Could not refresh the BIM.works token");
	}
	return await response.json();   // the auth object created by the backend
});

registerExpiredTokenHandler is a method of the client (Classic JavaScript SDK).

2. Know when it runs#

  • Before every request the client compares the token's expires_millis_utc with the clock. When the token has expired it calls the handler, waits for the new token, and then sends the request with it. Requests that arrive while a refresh is running wait for the same refresh.
  • The check is client-side and clock-based. A token the server rejects for another reason (deleted API token, wrong scope) does not trigger the handler; such errors reach your code as usual.
  • Without a handler, a request with an expired token fails with "No expiredTokenHandler is present".
  • createClientWithAuth itself refuses a token that has already expired (TOKEN_EXPIRED); the handler does not help there, so obtain a fresh token before creating the client.
Tip

Keep the token's lifetime short and the refresh cheap. A refresh is one call to your backend; a stolen token, on the other hand, works until it expires, because delegated tokens cannot be revoked.

3. In the other SDKs#

Java and .NET have no handler; they are meant for backends that log in with an API token, whose auth token lives 90 days. When such a client's token has expired (a service that runs for months), create a new client from the long-lived factory; the same applies to a Java or .NET client made from a delegated token with createClientWithAuth. Over HTTP, a 401 with TOKEN_EXPIRED means: log in again and retry.

Check#

Create a versions token with expires_seconds of 5, create the client, wait ten seconds, and run a query. The console logs "Token has expired" followed by "Calling expiredTokenHandler", the backend endpoint is called once, and the query answers. Drop the handler and repeat: the query fails instead.