# Multi-Factor Authentication at Login

Learn how users complete an MFA challenge on the hosted login pages or on your own pages with the Login API, including recovery codes.

> For the index of this section of the site, see [llms.txt](https://fusionauth.io/docs/llms.txt)

When FusionAuth decides to challenge a user, the user enters a code after the password. The [hosted login pages](https://fusionauth.io/docs/concepts/integration-points.md#hosted-login-pages) handle the challenge for you. When you build your own login pages, your pages handle the challenge with the [Login API](https://fusionauth.io/docs/concepts/integration-points.md#login-api).

To decide when FusionAuth challenges a user, see [Configure MFA](https://fusionauth.io/docs/authentication/multi-factor-authentication/configure.md).

## Hosted Login Pages

The hosted login pages use the following pages for the MFA challenge:

*   The method picker lists the user's MFA methods. The OAuth two-factor methods template renders the method picker.
*   The code page asks for the code. The OAuth two-factor template renders the code page.

### Choose the Method

After the password, FusionAuth either shows the method picker or picks a method and goes straight to the code page. The following diagram shows how FusionAuth decides:

```mermaid
flowchart TD
    password[User enters a password] --> lastUsed{Does the user have a last used method?}
    lastUsed --> |Yes| voice{Is the method SMS, and does the Tenant turn on SMS and voice?}
    lastUsed --> |No| authOnly{Are all of the user's methods authenticators?}
    authOnly --> |Yes| codePage[Code page]
    authOnly --> |No| picker[Method picker]
    voice --> |Yes| picker
    voice --> |No| send[Send a code for an email or SMS method]
    picker --> |User chooses a method| send
    send --> codePage
```

How the hosted login pages decide whether to show the MFA method picker.

The last used method is the method from the user's last successful MFA login. A user with one method always has a last used method, so FusionAuth skips the method picker. Adding a method doesn't make the method the last used method. A user with two methods sees the method picker until the first login with one of the methods.

When the user has one SMS method and the Tenant turns on both SMS and voice, FusionAuth shows the method picker anyway. The user then chooses between a text message and a voice call.

For an email or SMS method, FusionAuth sends the code before the code page appears. For an authenticator method, the user reads the code from the app.

The method picker lists each method by name, or by type when the method has no name. When the user has recovery codes left, the method picker also lists a recovery code option with the number of codes left:

![The MFA method picker on the hosted login pages, listing an email method, an authenticator method, and recovery codes.](https://fusionauth.io/img/docs/screenshots/authenticate-mfa-challenge-methods.png)

### Enter the Code

The code page asks the user to enter the verification code or a recovery code. The code page accepts a recovery code for every method, so the user doesn't need to choose the recovery code option first:

![The MFA code page on the hosted login pages, after FusionAuth sent a code by SMS.](https://fusionauth.io/img/docs/screenshots/authenticate-mfa-code-sent.png)

When the user has an email or SMS method, the code page shows a link back to the method picker. The user follows the link to resend a code, to choose another method, or to choose the recovery code option. A user with only authenticator methods never sees the method picker.

When the user selects the option to trust the computer, FusionAuth skips the challenge on the same browser until the trust expires. The Tenant's **Two-Factor trust** duration controls how long the trust lasts.

### Customize the Pages

To change the MFA pages, edit the OAuth two-factor methods and OAuth two-factor templates in an [advanced theme](https://fusionauth.io/docs/theme/advanced-themes.md). For email, SMS, and voice methods, also edit the templates for the messages that carry the code. For more information, see [Email Templates](https://fusionauth.io/docs/email/email-templates.md) and [Message Templates](https://fusionauth.io/docs/messengers/message-templates.md).

When you customize the pages, keep the following behavior in mind:

*   The recovery code option on the method picker is only a hint. Removing the option doesn't stop users from entering a recovery code on the code page.
*   The `code` message sets the text in the code field. When you change the message, keep the mention of recovery codes, because the text is the only hint for a user with only authenticator methods.
*   The **`recoverCodesAvailable`** variable holds the number of recovery codes the user has left.

For the variables on each page, see [OAuth Two-Factor Methods](https://fusionauth.io/docs/theme/advanced-themes/template-variables.md#oauth-two-factor-methods) and [OAuth Two-Factor](https://fusionauth.io/docs/theme/advanced-themes/template-variables.md#oauth-two-factor) in the template variables reference. To change the text without an advanced theme, see [Messages](https://fusionauth.io/docs/theme/localization.md#messages).

## Build Your Own Login Pages

When you build your own login pages, handle the MFA challenge with the Login API and the Two-Factor APIs.

1.  Call the [Login API](https://fusionauth.io/docs/apis/login.md) with the user's login Id and password:
    
    Log the user in
    
    ```shell
    API_KEY=...
    REQUEST_PAYLOAD='{...}'
    
    curl -XPOST -H 'Content-type: application/json' -H "Authorization: $API_KEY" 'https://local.fusionauth.io/api/login' -d $REQUEST_PAYLOAD
    ```
    
    Login request
    
    ```json
    {
      "applicationId": "10000000-0000-0002-0000-000000000001",
      "eventInfo": {
        "ipAddress": "127.0.0.1"
      },
      "loginId": "example@fusionauth.io",
      "loginIdTypes": [
        "email"
      ],
      "newDevice": false,
      "noJWT": false,
      "password": "password"
    }
    ```
    
    When FusionAuth challenges the user, the Login API returns a `242` status code. The response contains a **`twoFactorId`** and the user's **`methods`**:
    
    Login response when FusionAuth challenges the user
    
    ```json
    {
      "methods": [
        {
          "authenticator": {
            "algorithm": "HmacSHA1",
            "codeLength": 6,
            "timeStep": 30
          },
          "id": "2KSZ",
          "method": "authenticator"
        },
        {
          "email": "richard@fusionauth.io",
          "id": "KLRT",
          "method": "email"
        }
      ],
      "twoFactorId": "YkQY5Gsyo4RlfmDciBGRmvfj3RmatUqrbjoIZ19fmw4"
    }
    ```
    
    Store the **`twoFactorId`** for the next steps. The **`twoFactorId`** expires after the Tenant's **Two-Factor login** duration, five minutes by default.
    
2.  When the user has more than one method, show the **`methods`** and ask the user to choose one. Also offer a recovery code.
    
3.  When the user chooses an email or SMS method, send a code with the [Send a Multi-Factor Code During Login or Step Up API](https://fusionauth.io/docs/apis/two-factor/send-a-multi-factor-code-during-login-or-step-up.md). Set **`methodId`** to the method's **`id`**:
    
    Send a code
    
    ```shell
    REQUEST_PAYLOAD='{...}'
    TWO_FACTOR_ID=... # from the login response
    
    curl -XPOST -H 'Content-type: application/json' 'https://local.fusionauth.io/api/two-factor/send?twoFactorId='$TWO_FACTOR_ID -d $REQUEST_PAYLOAD
    ```
    
    Send a code request
    
    ```json
    {
      "messageType": "Voice",
      "methodId": "KLRT"
    }
    ```
    
    FusionAuth sends the code to the email address or phone number of the method, which can differ from the user's login Id. To resend a code, call the API again. Each send cancels the codes sent before.
    
4.  Build a page that collects the code. Accept a recovery code in the same field.
    
5.  Call the [Complete Multi-Factor Authentication API](https://fusionauth.io/docs/apis/login/complete-mfa.md) with the **`twoFactorId`** and the code:
    
    Complete the login
    
    ```shell
    API_KEY=...
    REQUEST_PAYLOAD='{...}'
    
    curl -XPOST -H 'Content-type: application/json' -H "Authorization: $API_KEY" 'https://local.fusionauth.io/api/two-factor/login' -d $REQUEST_PAYLOAD
    ```
    
    Complete the login request
    
    ```json
    {
      "applicationId": "10000000-0000-0002-0000-000000000001",
      "code": "915187",
      "ipAddress": "192.168.1.42",
      "twoFactorId": "YkQY5Gsyo4RlfmDciBGRmvfj3RmatUqrbjoIZ19fmw4"
    }
    ```
    
    The API returns `200` and the user's tokens when the code is valid, and `421` when the code is wrong.
    

To skip the challenge at later logins from the same device, set **`trustComputer`** to `true` in the complete request. The response then contains a **`twoFactorTrustId`**. Send the **`twoFactorTrustId`** with later Login API requests. For more information, see the [Login API](https://fusionauth.io/docs/apis/login.md).

## Troubleshooting

When FusionAuth doesn't send email codes, check the SMTP settings. For more information, see [Email Troubleshooting](https://fusionauth.io/docs/deploy/troubleshoot/troubleshooting.md#troubleshooting-email).

When FusionAuth doesn't send SMS or voice codes, test the messenger, turn on debugging for the messenger, and check the Event Log at System -> Event Log .

When a user has an MFA method but FusionAuth doesn't ask for a code at login, check the following:

*   The Tenant turns on the user's method. FusionAuth doesn't challenge a user whose only methods the Tenant turns off.
*   The login policy of the Tenant or the Application. A risk-based policy skips the challenge when the risk is below the policy level.
*   Whether the user chose to trust the device at an earlier login.

When FusionAuth asks for a code but can't send one, check that the Tenant turns on the method and the message type, such as SMS or voice. For more information, see [Turn Off an MFA Method](https://fusionauth.io/docs/authentication/multi-factor-authentication/contextual.md#turn-off-an-mfa-method).

Related

[APIs Complete Multi-Factor Authentication API API documentation for the FusionAuth Complete Multi-Factor Authentication API. login apimulti-factor authenticationmfa login](https://fusionauth.io/docs/apis/login/complete-mfa.md)

[Authentication Configure Multi-Factor Authentication Learn how to turn on MFA methods for a Tenant and set when FusionAuth asks users for a second factor. mfamulti-factor authenticationtwo-factor](https://fusionauth.io/docs/authentication/multi-factor-authentication/configure.md)

[Authentication Disable Multi-Factor Authentication for a User Learn how to remove MFA methods from a user with the account pages, the Admin UI, the Two-Factor APIs, or the User API. mfamulti-factor authenticationtwo-factor](https://fusionauth.io/docs/authentication/multi-factor-authentication/disable.md)

[Authentication Enable Multi-Factor Authentication for a User Learn how to add MFA methods to a user with the account pages, the Admin UI, the Two-Factor APIs, or the User API. mfamulti-factor authenticationtwo-factor](https://fusionauth.io/docs/authentication/multi-factor-authentication/enable.md)
---

## Other pages in Multi Factor Authentication

> For the full index of this section, see [Authentication](https://fusionauth.io/docs/llms-authentication.txt).

- [Configure Multi-Factor Authentication](https://fusionauth.io/docs/authentication/multi-factor-authentication/configure.md): Learn how to turn on MFA methods for a Tenant and set when FusionAuth asks users for a second factor.
- [Contextual Multi-Factor Authentication](https://fusionauth.io/docs/authentication/multi-factor-authentication/contextual.md): Learn how FusionAuth decides when to ask for MFA at login, including Tenant and Application policies, device trust, Intelligent MFA, and custom MFA logic.
- [Disable Multi-Factor Authentication for a User](https://fusionauth.io/docs/authentication/multi-factor-authentication/disable.md): Learn how to remove MFA methods from a user with the account pages, the Admin UI, the Two-Factor APIs, or the User API.
- [Enable Multi-Factor Authentication for a User](https://fusionauth.io/docs/authentication/multi-factor-authentication/enable.md): Learn how to add MFA methods to a user with the account pages, the Admin UI, the Two-Factor APIs, or the User API.
- [Multi-Factor Authentication (MFA)](https://fusionauth.io/docs/authentication/multi-factor-authentication.md): Learn about multi-factor authentication (MFA) in FusionAuth, including the supported methods, recovery codes, and trust tokens.
- [Add Step-Up Authentication With the Two-Factor APIs](https://fusionauth.io/docs/authentication/multi-factor-authentication/step-up-api.md): Learn how to ask a logged in user for another factor before a sensitive action, using the FusionAuth Two-Factor APIs.
- [Step-Up Authentication](https://fusionauth.io/docs/authentication/multi-factor-authentication/step-up.md): Learn about step-up authentication in FusionAuth, including how to choose an approach and how long a step-up stays valid.
