JavaScript SDK

Account hooks

Headless hooks for a profile page, active sessions, alternate phones and an MFA setup screen, from @scute/auth-ui-react.

These hooks hold the state for account pages; you draw the UI. They need AuthContextProvider above them.

useUserProfile#

import { useUserProfile } from "@scute/auth-ui-react";function Profile() {  const { user, isUpdating, error, updateMeta } = useUserProfile();  const save = async (company: string) => {    const result = await updateMeta({ company });    if (!result.ok) console.log(result.error.fieldErrors);  };  // ...}
Field
user, isLoading, isAuthenticatedFrom useAuth, with a fresher copy after refetch or updateMeta
isUpdating, errorupdateMeta state
updateMeta(meta)Returns { ok: true } or { ok: false, error: { code: "validation_error" | "unknown", message, fieldErrors? } }. fieldErrors maps field names to messages.
refetch()Reads the user again
signOut()Signs out

useSessions#

import { useSessions } from "@scute/auth-ui-react";function Sessions() {  const { sessions, revoke, isCurrent } = useSessions();  return (    <ul>      {sessions.map((s) => (        <li key={s.id}>          {s.browser} on {s.platform} {isCurrent(s.id) && "(this device)"}          <button onClick={() => revoke(s.id)}>Sign out</button>        </li>      ))}    </ul>  );}

{ sessions, loading, error, refetch, revoke, isCurrent }. revoke(id) returns { ok: true, data: {} } or { ok: false, error: { code, message } } and refetches the list; revoking this device's session signs out locally too.

useAlternatePhones#

const { phones, add, verify, remove, loading, error, refetch } = useAlternatePhones();const started = await add("+14155550100", "work");if (started.ok) await verify(started.data.challengeToken, code);
ActionReturns
add(phone, label?){ ok: true, data: { challengeToken, phone } }. An SMS code goes to the number; nothing is saved yet.
verify(challengeToken, code){ ok: true, data: {} }. The number is added and the list refetched.
remove(phone){ ok: true, data: {} }. No re-verification needed.

Failures are { ok: false, error: { code, message } } with code one of invalid_phone, phone_is_canonical, phone_already_registered, alternate_phone_cap_exceeded, alternate_phone_not_found, unknown.

useSecureAccount#

One "secure your account" screen: the MFA methods the app allows, which the user has, and inline setup for one method at a time.

import { useSecureAccount } from "@scute/auth-ui-react";function SecureAccount() {  const { methods, activeMethod, phase, provisioningUri, backupCodes, startEnroll, submitCode, removeMethod } =    useSecureAccount();  return methods.filter((m) => m.allowed).map((m) => (    <section key={m.key}>      <h3>{m.label}</h3>      <p>{m.description}</p>      {m.enrolled        ? <button onClick={() => removeMethod(m.key)}>Remove</button>        : <button onClick={() => startEnroll(m.key)}>Set up</button>}    </section>  ));}
Field
methods{ key, label, description, allowed, enrolled, count?, enrollmentId? }[], ordered passkey, totp, sms, email, backup_codes
loading, isAuthenticatedSigned out, every method reads enrolled: false
activeMethod, phaseThe method being set up; phase is "idle", "enrolling", "pending_verify", "verifying", "verified" or "error"
error{ code, message }; code is not_authenticated, method_not_allowed, enrollment_failed, verify_failed, remove_failed, passkey_failed, backup_codes_failed, config_load_failed or unknown
provisioningUri, secretTOTP, cleared after verify
backupCodesPlain codes after startEnroll("backup_codes"), until clearBackupCodes()
Action
startEnroll(key, { secret_data?, name? })passkey opens the browser prompt; totp, sms, email move to pending_verify; backup_codes generates codes
submitCode(code)Verifies a TOTP, SMS or email code
cancelEnroll()Back to idle, dropping the secret and codes. Also runs on sign-out.
removeMethod(key)Removes an enrolled method
clearBackupCodes()Drops the plain codes
refresh()Reads the app settings and the user's methods again