# Multi-factor authentication (TOTP)

Source: https://cubibase.com/docs/auth/mfa

Also available: [Markdown version](https://cubibase.com/docs/auth/mfa.md) · [llms.txt](https://cubibase.com/llms.txt) · [full site content](https://cubibase.com/llms-full.txt)

Users enroll an authenticator app; sign-in then returns a challenge that a six-digit code completes. MFA can be optional per user or required for the whole project.

## Enable MFA for the project

1. **Open the MFA settings** — Console → your project → Auth → Multi-factor.
2. **Enable MFA** — Users can now enroll a TOTP authenticator. Optionally make MFA required: every session must then include a verified factor.

## Enroll an authenticator

`POST /auth/v1/factors` with the bearer token returns a TOTP secret and an `otpauth://` URI. Render the URI as a QR code (or show the secret for manual entry), then confirm the first code with `POST /auth/v1/factors/verify`.

**1 · Enroll · response**

```json
{
  "factorId": "b2e6f0a4-7c19-4d83-9e51-6a0c2d8b4f37",
  "secret": "<TOTP_SECRET>",
  "uri": "otpauth://totp/CubiBase%20example-project:mina%40example.com?secret=…&issuer=…"
}
```

**2 · Verify the first code**

```http
POST https://example-project.api.cubibase.com/auth/v1/factors/verify
apikey: cb_pub_EXAMPLE_ONLY
Authorization: Bearer <ACCESS_JWT>
Content-Type: application/json

{
  "factorId": "b2e6f0a4-7c19-4d83-9e51-6a0c2d8b4f37",
  "code": "123456"
}
```

> **note**: A factor only counts after the first code verifies. Remove a factor with `DELETE /auth/v1/factors/{factorId}`; the Console can also remove factors for a user.

## Sign-in with a challenge

When a user with a verified factor signs in with a password, the token grant returns an MFA challenge instead of a session:

**Password grant · MFA challenge**

```json
{
  "mfaRequired": true,
  "challengeId": "9d41c7a2-5b18-4e6f-a0d3-2e7b9c4f1a58",
  "factorId": "b2e6f0a4-7c19-4d83-9e51-6a0c2d8b4f37",
  "expiresAt": "2026-11-18T09:17:44.000Z"
}
```

Post the challenge id and the current code to complete the sign-in. Challenges expire after five minutes and are single-use.

**Complete the challenge**

```http
POST https://example-project.api.cubibase.com/auth/v1/token?grant_type=mfa
apikey: cb_pub_EXAMPLE_ONLY
Content-Type: application/json

{
  "challengeId": "9d41c7a2-5b18-4e6f-a0d3-2e7b9c4f1a58",
  "code": "123456"
}
```

The response is a normal session whose tokens carry `amr: ["pwd", "mfa"]` — the proof Data API requests check when the project requires MFA.

## Required MFA

- Every session must include a verified factor; sessions without it fail Data API calls with `401 mfa_required`.
- A user without a factor cannot sign in until they enroll: the password grant returns `403 mfa_enrollment_required`.
- Google sign-in does not replace MFA for users with a verified factor — they must use password + TOTP.
- Code attempts are rate-limited (10 per minute per IP by default).

## Additional resources

- [Sessions and tokens](https://cubibase.com/docs/auth/sessions) How the amr claim travels with the session.
- [Email and password](https://cubibase.com/docs/auth/email-password) The grant that starts the challenge.
