Two-step verification
A user who turns on two-step verification enters a second code after they sign in with a password, an email link or an email code. The second code comes from an authenticator app, from one of the user’s backup codes or, when you turn it on, from an email.
Turn on two-step verification
To turn on two-step verification, add twoFactor with "enabled": true to plugins in the auth block of amplify.jsonc, next to your other auth settings. twoFactor.enabled turns on authenticator app codes and backup codes. To also accept a code sent by email as the second step, set otp.enabled to true. For how the auth block works and how to apply your changes, see Auth configuration.
// amplify.jsonc { "backend": { "auth": { "appName": "Acme Notes", "emailAndPassword": { "enabled": true }, "plugins": { "twoFactor": { "enabled": true, "otp": { "enabled": true } } } } } }
Authenticator apps list your app under its appName. On the managed sign-in page, users enter the second step after they sign in. The two-step calls in the client SDK return an error until you turn two-step verification on.
Set up two-step verification for a user
To turn on two-step verification for a signed-in user, call twoFactor.enable with the user’s password. A user who signed up only through a social provider has no password, and must set one before they can turn on two-step verification.
const { data, error } = await backend.auth.twoFactor.enable({ password, // required; the user's current password issuer, // optional; the name that authenticator apps show; defaults to your appName }) // data.totpURI: the otpauth:// URI for the authenticator app; data.backupCodes: 10 backup codes
Show totpURI to the user as a QR code, so they can add your app to their authenticator app. Then call twoFactor.verifyTotp with a code from the app. Two-step verification is on for the user after the backend accepts that code. The backend stores the backup codes encrypted. Show the 10 backup codes to the user, and ask them to keep the codes.
With otp.enabled on, a signed-in user can instead turn on two-step verification with the emailed code only: call twoFactor.sendOtp, then twoFactor.verifyOtp with the code.
To manage a user’s two-step verification later, call these, each with the user’s password:
-
twoFactor.getTotpUri({ password })– Returns the user’s authenticator app URI again. -
twoFactor.generateBackupCodes({ password })– Creates new backup codes and replaces the old ones. -
twoFactor.disable({ password })– Turns off two-step verification for the user.
When a user whose address isn’t verified signs in with an email link or an email code for the first time, the backend deletes the user’s two-step verification setup. To keep the setup, have users verify their email address before they turn on two-step verification. For more information, see Passwordless sign-in.
Sign in with the second step
When a user with two-step verification signs in with signIn.email or signIn.emailOtp, the call returns twoFactorRedirect: true and twoFactorMethods, the second steps that the user can take. It returns no session.
const { data, error } = await backend.auth.signIn.email({ email, password }) // data.twoFactorRedirect: true; data.twoFactorMethods: for example ['totp', 'otp']
totp is a code from the authenticator app. otp is the emailed code, listed when otp.enabled is on and the user signed in with a password.
To finish the sign-in with a code from the authenticator app, call twoFactor.verifyTotp. For a backup code, call twoFactor.verifyBackupCode.
const { data, error } = await backend.auth.twoFactor.verifyTotp({ code, // required; the code from the authenticator app trustDevice, // optional; true skips the second step in this browser at later sign-ins }) await backend.auth.twoFactor.verifyBackupCode({ code, // required; one of the user's backup codes trustDevice, // optional })
When a user signs in from a browser that they trusted with trustDevice, the backend skips the second step, after a password, an email link or an email code. Users who sign in on the managed sign-in page enter the second step at every sign-in.
Send the second code by email
With otp.enabled on, a user who signed in with a password can get the second code by email. To send the code, call twoFactor.sendOtp. When the user types the code into your app, call twoFactor.verifyOtp.
await backend.auth.twoFactor.sendOtp({ trustDevice }) // trustDevice is optional const { data, error } = await backend.auth.twoFactor.verifyOtp({ code, // required; the code from the email trustDevice, // optional })
An emailed code expires after 3 minutes. A user gets 5 tries for each emailed code. After 5 wrong codes, the backend refuses that code. To let the user try again, call twoFactor.sendOtp for a new code.
The backend sends the code in the one-time code email. To change that email, see Auth emails. Each emailed code counts toward the auth email limits. For the limits, see Quotas. On the local backend, the code arrives in Mailpit, unless the root auth block sets your own SMTP server. For more information, see Local auth and emails.
After an email link or an email code, the backend refuses the emailed second code with EMAILED_SECOND_FACTOR_REFUSED. The user finishes with an authenticator app code or a backup code. A user whose only second step is the emailed code signs in with their password, then enters the emailed code.
Finish the second step after an email link
Pass a callbackURL on a trusted origin to signIn.magicLink. Without one, the backend refuses the sign-in for a user with two-step verification.
For a user with two-step verification, the browser opens the link’s callbackURL. The URL has error=TWO_FACTOR_REQUIRED and two_factor_methods. On that page, call auth.getRedirectResult() to read the step, then call twoFactor.verifyTotp or twoFactor.verifyBackupCode.
In token mode, auth.getRedirectResult() returns a TWO_FACTOR_REQUIRED error, and your app can’t finish the step. Offer the user a password sign-in or an email code. For more information about token mode, see User sessions.
Settings
Set these in plugins.twoFactor in the auth block.
| Setting | Values | Default |
|---|---|---|
|
|
|
|
|
|
|
|
Errors
The client SDK returns each error’s code in error.code.
| Code | When | What to show |
|---|---|---|
|
|
An email link for a user with two-step verification, in token mode |
Sign in with a password or an email code |
|
|
An emailed second code after an email link or email code |
The authenticator app or backup code step |
|
|
An email link or email code for a user whose only second step is the emailed code, or an email link without a trusted |
Sign in with a password |
|
|
10 wrong second codes for one user. The backend refuses that user’s codes, the right one included, for up to 15 minutes |
Try again later, with |
|
|
The second step expired or ended, or the browser has no second step in progress |
Sign in again |
|
|
A wrong authenticator app code or emailed code |
Ask the user to try again |
|
|
A wrong or already used backup code |
Ask the user to try another backup code |
|
|
Too many wrong authenticator app or backup codes in one second step |
Sign in again |
|
|
A 6th try of an emailed code, after 5 wrong ones |
Send a new code with |
|
|
The emailed code expired or was used, no code was sent, or the code was refused after 5 wrong tries |
Send a new code with |
A correct code before the tenth wrong one resets the count. For a user with an authenticator app, the 10 wrong codes of any kind are in a row. For a user whose only second step is the emailed code, they’re within 15 minutes. This lock stays on when you turn off rateLimit.