JavaScript SDK

MFA hooks

useEnrollMfa, useMfaVerify, useFactorList and useBackupCodes from @scute/react-hooks.

Headless hooks over the client's MFA methods. They hold the UI state; the client holds the tokens. All need AuthContextProvider above them. The underlying calls are on MFA.

useEnrollMfa#

Enrolls a factor for the signed-in user: enroll, then verify.

import { useEnrollMfa } from "@scute/react-hooks";function AddAuthenticator() {  const { enroll, verify, state, provisioningUri, secret, error } = useEnrollMfa();  if (state === "idle") return <button onClick={() => enroll({ method: "totp" })}>Add an authenticator app</button>;  if (state === "verified") return <p>Authenticator added.</p>;  return (    <form onSubmit={(e) => { e.preventDefault(); verify(new FormData(e.currentTarget).get("code") as string); }}>      {provisioningUri && <QrCode value={provisioningUri} />}      <p>Or enter {secret}</p>      <input name="code" inputMode="numeric" />      {error && <p>{error.message}</p>}    </form>  );}

QrCode is your own component.

Field
state"idle", "enrolling", "pending_verify", "verifying", "verified" or "error"
enrollmentThe factor being set up
provisioningUri, secretTOTP only. Cleared after verify, on reset() and on sign-out.
error{ message, code? } or null
enroll(params){ method: "totp", name? }, { method: "sms", secret_data, name? } or { method: "email", secret_data?, name? }
verify(code)Checks the code
reset()Back to idle

useMfaVerify#

The second-factor step during sign-in. It reads scute.pendingMfaChallenge and picks up a challenge issued after it mounts (mfa_required, mfa_enrollment_required).

import { useMfaVerify } from "@scute/react-hooks";function MfaStep() {  const { verify, switchMethod, resend, state, error, nextStep } = useMfaVerify();  // verify(code) signs in, or sets nextStep when the sign-in needs another step}
Field
pendingChallenge, challengeTokenThe challenge being verified
state"idle", "verifying", "verified" or "error"
error{ message, code? } or null
nextStepThe next sign-in step when the code was right but the sign-in is not finished (a contact to confirm, a factor to set up)
verify(code)verifyMfaChallenge
switchMethod(method)Calls switchMfaMethod with one of the step's available methods (such as "totp", "email_otp", "sms_otp", "backup_code"). The old challenge is cancelled first; if the new one fails, pendingChallenge becomes null.
resend()Sends an email or SMS code again
cancel()Cancels the challenge; on an error the challenge is kept

useFactorList#

The signed-in user's factors. Loads on mount and when the user changes; empty while signed out.

const { factors, backupCodesAvailable, mfaEnabled, loading, error, refresh, remove, setDefault } = useFactorList();

remove(id, { challenge? }) and setDefault(id) refresh the list on success. When remove is refused for re-verification (needsReverification(error)), retry with a completed challenge's token.

useBackupCodes#

const { codes, generate, clear, loading, error } = useBackupCodes();

generate({ challenge? }) puts the plain codes in codes. Show them once, then call clear(). They are also dropped on sign-out.