> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.doc-detective.com/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.doc-detective.com/_mcp/server.

# Sign in with an authenticator app code

> Sign in to an account that uses MFA with an authenticator app, by generating the one-time code in your test with totp().

Many sign-ins ask for a six-digit code from an authenticator app after the password. That's multi-factor authentication (MFA), also called two-factor authentication (2FA). The app computes the code from a setup key and the clock, using the time-based one-time password (TOTP) standard. If your test holds the same setup key, Doc Detective's `totp()` expression computes the same code, with no phone involved.

Combine it with [session reuse](/docs/test-docs/reuse-a-session) so the run signs in, and uses a code, only once.

## Before you begin

* A test account outside production, with an authenticator app enrolled for it. Never use a real person's account. See [Agree on the account with your security owner](/docs/test-docs/sign-in#agree-on-the-account-with-your-security-owner).
* An authenticator that uses the common TOTP settings: SHA-1, 6 digits, and a 30-second period. `totp()` doesn't support SHA-256, SHA-512, 8-digit, or 60-second codes.

## Get the setup key

The setup key is the base32 secret behind the QR code that you scan when you enroll an authenticator app. To get it, enroll the test account yourself:

1. Sign in as the test account, and start adding an authenticator app in the account's security settings.
2. On the page that shows the QR code, choose the option to enter the key manually. Its label varies by product, such as **Can't scan it?** or **Enter a setup key**. The page shows the key, a string of letters `A`–`Z` and digits `2`–`7`, such as `JBSWY3DPEHPK3PXP`.
3. Store the key as a CI secret, such as `TOTP_SECRET`, and in your local `.env` file. Spaces, lowercase letters, and `=` padding in the key are fine.
4. Finish the enrollment with a code from an authenticator app that scans the same QR code. Codes from the app and from `totp()` match, because both come from the same key.

If the page shows only a QR code, the key is in the `secret` parameter of the `otpauth://` link that the QR code encodes. If your engineering team seeds test accounts with a known key, use that key instead of enrolling one.

Re-enrolling the account creates a new key and stops the old one from working, so update the secret whenever someone re-enrolls the account.

## Sign in with the code

Generate the code in the `variables` of a step, then type the variable in a later step. A step's `variables` field sets environment variables from expressions after the step's action runs, and later steps read them as `$NAME`. `totp()` works in a step's `variables` and in conditions, not directly in `type` or other step fields: `"type": "totp(\"$TOTP_SECRET\")"` types that text literally.

This `beforeAll` sign-in spec signs in with a password and a code, then saves the session for the rest of the run:

**`tests/sign-in.spec.json`**

```json title="tests/sign-in.spec.json"
{
  "autoRecord": false,
  "tests": [
    {
      "testId": "sign-in-mfa",
      "steps": [
        { "goTo": "https://app.example.com/login" },
        { "find": { "selector": "#user", "click": true, "type": "$APP_USER" } },
        { "find": { "selector": "#password", "click": true, "type": ["$APP_PASSWORD", "$ENTER$"] } },
        {
          "description": "Wait for the code prompt, then generate the code.",
          "find": { "selector": "#code" },
          "variables": { "OTP_CODE": "totp(\"$TOTP_SECRET\")" }
        },
        { "type": { "keys": ["$OTP_CODE", "$ENTER$"], "selector": "#code" } },
        { "find": "Dashboard" },
        { "saveState": { "path": "auth.json", "directory": "../.auth", "overwrite": true } }
      ]
    }
  ]
}
```

A step's `variables` run after its action, so `totp()` generates the code once the code field appears, right before the test types it. Codes change every 30 seconds. If fewer than 3 seconds remain in the current window, `totp()` waits for the next one, so the code doesn't expire before the form submits. Doc Detective masks the secret and the code in logs, reports, and screenshots.

List the spec in `beforeAll` in your config. Then load the session in each test. See [Sign in once and reuse the session](/docs/test-docs/reuse-a-session). For how to pass `TOTP_SECRET` from a CI secret, see [Protect sign-in secrets and session files](/docs/test-docs/sign-in-secrets#store-credentials-as-ci-secrets).

## Use a fixed one-time code

Some non-production environments accept a fixed one-time code for test accounts. Then you don't need `totp()`. Store the code like a password, such as in `OTP_CODE`, and type it:

```json
{ "type": { "keys": ["$OTP_CODE", "$ENTER$"], "selector": "#code" } }
```

Keep a fixed code out of production. See [Agree on the account with your security owner](/docs/test-docs/sign-in#agree-on-the-account-with-your-security-owner).

## Related

* [`totp()`](/reference/reference/expressions#function-operators) in the expressions reference.
* [A one-time code is rejected](/docs/test-docs/sign-in-troubleshooting#a-one-time-code-is-rejected).