JavaScript SDK

Impersonation

Sign in as a user for support, from your backend and in the browser, with a banner and server-side limits.

Support staff can sign in as a user. The session is short (the app's maximum, default 60 minutes), never refreshed, and its token names who is really acting in an act claim. The app setting impersonation must be on.

Start from your backend#

import { ScuteAdminApi } from "@scute/js-core";const admin = new ScuteAdminApi({ appId: process.env.SCUTE_APP_ID, secretKey: process.env.SCUTE_SECRET });const { data: tokens, error } = await admin.impersonateUser(userId, {  reason: "Ticket 4821: cannot see invoices",  minutes: 30,  actor: { email: "support@acme.com", name: "Sam" },});// tokens: { access, access_expires_at, session_id, user_id, impersonation }
reasonstringrequired
Why. Shown in the user's audit trail.
minutesnumber
Up to the app's maximum (default 60).
actorUserIdstring | number
One of the app's users who holds user:impersonate on this user.
actor{ email?, name?, id? }
Or the person, named by your backend.
challengestring
A completed step-up challenge's token, when user:impersonate asks for one.
approvalstring | number
An approved request's ID, when user:impersonate needs approval.

The answer holds an access token, never a refresh token.

Switch the browser#

Hand the tokens to the support person's browser:

await scute.beginImpersonation(tokens);

The support person's own session is kept aside (the refresh token in an httpOnly cookie on the server, or in storage in a browser). signed_in fires with the user.

const imp = await scute.getImpersonation();// { actor: { kind: "app_user" | "operator" | "backend", sub?, email?, name? }, expiresAt: Date } or nullawait scute.stopImpersonating(); // ends it and brings the own session back; false when there was nothing to stop

Instead of passing tokens, your backend can make a one-time link into your app:

const { data } = await admin.createImpersonationHandoff(userId, { reason: "Ticket 4821" });// data.handoff: { code, url, expires_at }

url is your app's sign-in page with ?sct_imp=<code> (null when the app has no address set). The code works once, for a couple of minutes. On that page, scute.signInWithImpersonationHandoff() swaps the code for the session; AuthContextProvider in @scute/react-hooks does this on its own and removes the code from the address bar. getImpersonationHandoffCode(url?) reads the code.

List and end sessions#

const { data: open } = await admin.listImpersonations(userId);// [{ session_id, actor, reason, started_at, expires_at }]await admin.stopImpersonating(userId);            // all of themawait admin.stopImpersonating(userId, sessionId); // one

Limit what impersonators can do#

Permissions marked "blocked while impersonating" are refused when the check's context says so. On your server, pass the verified token's claims through impersonationContext:

import { impersonationContext } from "@scute/js-core";// claims: the payload of the request's access token, verified by your serverasync function canDeleteAccount(userId: string, claims: { imp?: unknown; act?: unknown }) {  const { data } = await admin.authzCheck({    userId,    action: "delete",    resource: "account",    context: impersonationContext(claims), // { impersonated: true, actor } when claims.imp is true, else {}  });  return data?.allowed === true;}

decideLocally and ScuteLocalAuthz refuse the same permissions when context.impersonated is set. decodeImpersonation(token) reads the act claim for display; it does not verify the token.

For a React banner, see useImpersonation.