> ## Documentation Index
> Fetch the complete documentation index at: https://www.dynamic.xyz/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Multi-Factor Authentication

> Implement MFA with TOTP and recovery codes in Unity.

## TOTP setup

### Add a TOTP device

```csharp theme={"system"}
var result = await DynamicSDK.Instance.Mfa.AddDevice("totp");

// result.Secret — base32 secret for manual entry
// result.Uri    — otpauth:// URI for QR code

Debug.Log($"TOTP Secret: {result.Secret}");
Debug.Log($"TOTP URI: {result.Uri}");
```

### Verify TOTP device

After the user enters a code from their authenticator app:

```csharp theme={"system"}
await DynamicSDK.Instance.Mfa.VerifyDevice(code, "totp");
```

## TOTP authentication

Authenticate with a TOTP code:

```csharp theme={"system"}
string mfaToken = await DynamicSDK.Instance.Mfa.AuthenticateDevice(
    new MfaAuthenticateDevice
    {
        Code = "123456",
        Type = "totp",
        DeviceId = deviceId,
        CreateMfaToken = new MfaCreateToken { SingleUse = true },
    });
```

## Recovery codes

Generate recovery codes for account recovery:

```csharp theme={"system"}
List<string> codes = await DynamicSDK.Instance.Mfa.GetNewRecoveryCodes();

foreach (var code in codes)
{
    Debug.Log($"Recovery code: {code}");
}
```

<Warning>
  Store recovery codes securely. They can be used to recover the account if the user loses access to their TOTP device.
</Warning>

## Complete example

```csharp theme={"system"}
using DynamicSDK.Core;
using UnityEngine;
using System.Collections.Generic;

public class MfaManager : MonoBehaviour
{
    private string _deviceId;
    
    public async void SetupTotp()
    {
        try
        {
            var result = await DynamicSDK.Instance.Mfa.AddDevice("totp");
            
            Debug.Log("TOTP Setup:");
            Debug.Log($"Secret: {result.Secret}");
            Debug.Log($"URI (for QR code): {result.Uri}");
            
            // Store device ID for later use
            _deviceId = result.DeviceId;
            
            // Display QR code to user (use result.Uri)
            // User should scan with authenticator app
        }
        catch (System.Exception ex)
        {
            Debug.LogError($"TOTP setup failed: {ex.Message}");
        }
    }
    
    public async void VerifyTotp(string code)
    {
        try
        {
            await DynamicSDK.Instance.Mfa.VerifyDevice(code, "totp");
            Debug.Log("TOTP device verified successfully!");
        }
        catch (System.Exception ex)
        {
            Debug.LogError($"Verification failed: {ex.Message}");
        }
    }
    
    public async void AuthenticateWithTotp(string code)
    {
        try
        {
            string mfaToken = await DynamicSDK.Instance.Mfa.AuthenticateDevice(
                new MfaAuthenticateDevice
                {
                    Code = code,
                    Type = "totp",
                    DeviceId = _deviceId,
                    CreateMfaToken = new MfaCreateToken { SingleUse = true },
                });
            
            Debug.Log($"MFA authentication successful");
            Debug.Log($"Token: {mfaToken}");
        }
        catch (System.Exception ex)
        {
            Debug.LogError($"MFA authentication failed: {ex.Message}");
        }
    }
    
    public async void GenerateRecoveryCodes()
    {
        try
        {
            List<string> codes = await DynamicSDK.Instance.Mfa.GetNewRecoveryCodes();
            
            Debug.Log("Recovery codes generated:");
            foreach (var code in codes)
            {
                Debug.Log($"  {code}");
            }
            
            // Display codes to user and prompt them to save securely
        }
        catch (System.Exception ex)
        {
            Debug.LogError($"Failed to generate recovery codes: {ex.Message}");
        }
    }
}
```

## Next steps

* [Passkeys](/docs/unity/passkeys) - Passwordless authentication
* [Authentication](/docs/unity/authentication) - Other authentication methods
