Users and sign-in
Two-factor authentication
Fox Login supports time-based one-time passwords (TOTP) from authenticator apps such as Google Authenticator, 1Password or Authy. Users turn it on for their own account. Each user who turns it on also gets 10 one-time recovery codes.
Sign-in when TOTP is on
When a user has TOTP on, signIn, verifySignUp, signInWithCode and confirmPasswordReset return { status: "mfa_required", mfaToken } instead of a session, and the Google sign-in calls answer with mfa_token in the same case. The password, code or Google account was right; Fox Login now needs the second factor. Ask the user for the six-digit code from their authenticator app, or one of their recovery codes, and call verifyMfa.
const result = await fox.signIn({ email, password });
if (result.status === "mfa_required") {
const code = await askUser("Enter the 6-digit code from your authenticator app");
const user = await fox.verifyMfa({ mfaToken: result.mfaToken, code });
showApp(user);
} else {
showApp(result.user);
}The SDK does not store mfaToken; keep it in memory until the user enters the code. The challenge lasts 5 minutes.
| Situation | Response |
|---|---|
| Wrong code | 400 invalid_mfa_code. The user can try again. |
| 5 wrong codes, the 5 minutes passed, or the challenge was already used | 401 invalid_mfa_token. The user signs in again from the start. |
| Right code | 200 session. verifyMfa resolves to the user. |
Each recovery code works once.
Turn TOTP on for a user
A signed-in user turns TOTP on with two calls that authenticate with their access token. The JavaScript SDK has no methods for them yet, so call them with fetch.
const token = await fox.getToken();
const post = (path: string, body?: object) =>
fetch(`${fox.baseUrl}/v1/client/mfa/totp/${path}`, {
method: "POST",
headers: { Authorization: `Bearer ${token}`, "Content-Type": "application/json" },
body: body && JSON.stringify(body),
});
// 1. Get a secret. Show otpauth_url as a QR code and secret as a manual-entry fallback.
const { secret, otpauth_url } = await (await post("setup")).json();
// 2. The user scans the QR code and types the first code. TOTP is on from here.
const { recovery_codes } = await (await post("enable", { code })).json();setup does not turn anything on. Calling it again replaces the pending secret. enable turns TOTP on after the first correct code and returns 10 recovery codes, in that response only. Show them to the user once and ask them to save the codes somewhere safe.
Turn TOTP off
POST /v1/client/mfa/totp/disable with { "code": "…" } turns TOTP off and answers 204. The code can be a current TOTP code or a recovery code. All recovery codes are deleted along with the TOTP secret.
Errors
| Code | Status | When |
|---|---|---|
invalid_mfa_code |
400 | The TOTP or recovery code is wrong. |
invalid_mfa_token |
401 | The sign-in challenge expired, was used, or had 5 wrong codes. |
mfa_already_enabled |
409 | setup or enable for a user who already has TOTP on. |
Events
Turning TOTP on records mfa.enrolled, turning it off records mfa.removed, and every sign-in step that asks for the second factor records sign_in.mfa_required. You can read them in the event log or receive them as webhooks.