JavaScript SDK

MFA

The second factor during sign-in, enrollment, managing factors, backup codes, re-verification and trusted devices.

MFA policy (off, optional or required, and the grace period) is set per app in the dashboard. The client handles three cases: a sign-in that needs a second factor, a sign-in that needs one set up first, and a signed-in user managing their factors.

During sign-in#

When the first factor passes and MFA applies, a sign-in method returns a step instead of signing in:

{  mfaRequired: true,  mfaEnrollmentRequired: boolean,   // a factor has to be set up first  mfaGracePeriod: boolean,  mfaGraceDaysRemaining?: number,  mfaChallenge?: { token, status, purpose, method, expires_at, remaining_attempts?, time_remaining? },  availableMethods: string[],  setupToken: string | null,        // with mfaEnrollmentRequired  setupExpiresAt: string | null,}

mfa_required fires (or mfa_enrollment_required), and scute.pendingMfaChallenge holds the raw answer.

Verify the code#

const token = step.mfaChallenge.token;const result = await scute.verifyMfaChallenge(token, code, { trustDevice: true });if (result.error) return showError(result.error.message);if (result.data) return showNextStep(result.data); // e.g. a contact to confirm// signed in: signed_in fired

code is a TOTP, an email or SMS code, or a backup code (XXXX-XXXX). trustDevice is sent only when it is a boolean, and only matters when the app allows device trust (appData.device_trust_enabled); the result then has deviceTrusted: boolean.

Switch method, resend, cancel#

MethodDoes
switchMfaMethod(currentToken, method)Cancels the challenge and creates one for method ("totp", "email_otp", "sms_otp", "backup_code"). Returns { data: challenge } and updates pendingMfaChallenge.
resendChallenge(token)Sends an email, SMS or link challenge again.
cancelChallenge(token)Cancels it. Clears pendingMfaChallenge when it was that challenge.
getChallengeStatus(token)Returns { challenge } with its status.

Set up a factor while signing in#

With mfaEnrollmentRequired: true the user has no usable factor. The step carries a setup-only token that works on the enroll routes and nowhere else:

const { data } = await scute.enrollMfaWithSetupToken(step.setupToken, "totp");// data.provisioning_uri (otpauth://) for a QR code, data.secret for manual entryconst result = await scute.verifyMfaEnrollmentWithSetupToken(step.setupToken, data.enrollment.id, code);// signed in (data null), or the next step

enrollMfaWithSetupToken(setupToken, method, secretData?, name?) takes "totp", "sms" or "email"; for SMS and email, secretData is the phone or the email and a code is sent to it. Errors: 401 setup_expired (sign in again), 422 method_not_allowed, 422 invalid_code on verify.

Grace period nudge#

A sign-in during the grace period, or with optional MFA whose factors the app no longer accepts, signs in and fires mfa_enrollment_suggested before signed_in. scute.pendingMfaEnrollmentSuggestion holds:

{ mfa_grace_days_remaining?: number, available_methods: string[], mfa_factor_not_allowed: boolean }

Manage factors#

These need a signed-in user.

const { data } = await scute.listMfaMethods();// data.methods: [{ id, method, name, verified, is_default, last_used_at, created_at }]// data.backup_codes_available, data.mfa_enabledconst status = await scute.getMfaStatus();             // the signed-in userconst before = await scute.getMfaStatus("ada@acme.com"); // works before sign-in// { mfa_required, mfa_enabled, available_methods, in_grace_period? }

Enroll#

const { data, error } = await scute.enrollMfa({ method: "totp", name: "Phone" });// data.enrollment, data.provisioning_uri, data.secret (both shown once)await scute.verifyMfaEnrollment(data.enrollment.id, code);

enrollMfa params: method ("totp", "sms", "email"), secret_data (phone or email), name, challenge.

Remove, set default, backup codes#

await scute.removeMfaMethod(id);await scute.setDefaultMfaMethod(id);const { data } = await scute.generateBackupCodes(); // data.backup_codes: string[]

generateBackupCodes returns the plain codes once and invalidates the unused ones.

Re-verification#

Removing a factor, new backup codes, and adding a factor when one is already set up need a recent sign-in: within the app's mfa_reverify_minutes (default 10). After that the API refuses with error_code: "verification_required".

import { needsReverification } from "@scute/js-core";const { error } = await scute.removeMfaMethod(id);if (needsReverification(error)) {  // Ask the user to sign in again, or complete a step-up or MFA challenge  // and retry with its token:  await scute.removeMfaMethod(id, { challenge: challengeToken });}

removeMfaMethod(id, { challenge }), generateBackupCodes({ challenge }) and enrollMfa({ ..., challenge }) take the token of a completed challenge.

Trusted devices#

A trusted device skips the new-device check for the app's device_trust_days. Trust it on the MFA step (verifyMfaChallenge(..., { trustDevice: true })), or afterwards from a session whose sign-in passed MFA:

const { data } = await scute.listTrustedDevices();// data.devices: [{ id, label, city, country_code, first_seen_at, last_seen_at, trusted, trusted_at, trust_expires_at, current }]// data.current_device: { id, trusted, can_trust, reason }// data.device_trust: { enabled, days }const { data: device, error } = await scute.trustThisDevice();await scute.untrustDevice(id);

trustThisDevice is refused with device_trust_off, second_factor_required (the sign-in did not pass MFA) or device_unknown (no device fingerprint). current_device.reason carries the same codes before you try.