Skip to Content
Using the client

Updating the user

Replace the signed-in user without ending the session, and handle each way it can fail.

After a profile edit, the signed-in user has changed but the session hasn't. Signing in again would be wrong: the tokens are still good. auth.updateUser replaces the user, saves it and keeps the session, so every component that reads the user shows the change.

This page shows the two ways to call it, what happens when calls overlap, and each way it can fail.

Pass the new user

When you already have the new user, such as after your backend saved a profile form, pass it to updateUser:

src/rename.ts
import {  } from "./api.js";
import {  } from "./auth.js";

/** Saves a new first name, then shows it without signing in again. */
export async function (: string) {
  const  = await .();
  if (!) return;
  const  = await (`users/${..}`, {
    : "PUT",
    : { "Content-Type": "application/json" },
    : .({  }),
  });
  if (!.) throw new (`Rename failed (${.})`);
  await .({ ....,  });
}

The user is checked against your user schema, as on sign-in. The session ID stays the same, so data you cached under it stays valid.

Fetch the new user

When the new user comes from a request, pass a function that fetches it instead. fetchProfile is the request from Getting started:

src/profile.ts
import {  } from "./api.js";
import {  } from "./auth.js";

export const  = () => .();

The client notes the session before the function runs. If the user signs out or switches account while it runs, updateUser rejects with SESSION_CHANGED, and the user it fetched never lands on another account. The function has 15 seconds to finish.

Overlapping calls

When updateUser calls overlap, such as two quick saves of a form, the latest call wins. The earlier ones reject with SESSION_CHANGED, so an older user never replaces a newer one.

A token refresh that runs at the same time keeps the updated user, even when your refresh callback returns a user of its own.

When updateUser rejects

updateUser rejects rather than save a user to the wrong session. Here's when, and with what:

When…updateUser rejects with…
No one is signed inUNAUTHENTICATED
The session is lockedLOCKED
The access token expired, and refreshing it failedThat failure, such as REFRESH_FAILED
The saved session can't be readPERSISTENCE_FAILED
The user schema rejects the userUSER_VALIDATION_FAILED
The function rejects, or takes more than 15 secondsUSER_VALIDATION_FAILED, with the cause
The user signs out or switches account, or the access token expires, while the function runsSESSION_CHANGED
A newer updateUser call startsSESSION_CHANGED
The new user can't be savedPERSISTENCE_FAILED, and the user is signed out

Only the last case changes the session. In the others, updateUser leaves the user as it was.

A function that rejects with an AuthError keeps its code. If the authFetch call in fetchProfile rejects with LOCKED, for example, so does updateUser.

With users from the access token

A client that reads the user from the access token, with fromAccessToken, has no updateUser. The token is the only source of the user there, so call auth.refresh() to pick up a change.

Next steps

  • State: scope cached data to sessionId, which an update keeps.
  • Errors: show a failure to the user.
  • AuthClient reference: updateUser and the client's other methods.

On this page