

# Passwordless sign-in

Your users can sign in without a password, with a link or a one-time code that Amplify backend auth emails to them. Email links and email codes are off by default, and you turn on each one in the `plugins` object of the `auth` block in `amplify.jsonc`.

## Turn on passwordless sign-in
<a name="backend-auth-passwordless-turn-on"></a>

Add `magicLink` for email links, `emailOtp` for email codes, or both, to `plugins`, each with `"enabled": true`, next to your other auth settings:

```jsonc
// amplify.jsonc
{
  "backend": {
    "auth": {
      "appName": "Acme Notes",
      "emailAndPassword": { "enabled": true },
      "plugins": {
        "magicLink": { "enabled": true },
        "emailOtp": { "enabled": true }
      }
    }
  }
}
```

For how the `auth` block works and how to apply your changes, see [Auth configuration](backend-auth-config.md).

The backend sends the sign-in link and the code as auth emails, with your `appName` in them. To change their wording or send them from your own address, see [Auth emails](backend-auth-email.md). On the local backend, they arrive in Mailpit, unless the root `auth` block sets your own SMTP server. For more information, see [Local auth and emails](backend-local-auth.md).

Users of the [managed sign-in page](backend-auth-managed-sign-in.md) can sign in with each method that you turn on. On your own sign-in screens, call `auth.getMethods()` to find out which methods the backend serves.

## Sign in with an email link
<a name="backend-auth-passwordless-link"></a>

To email a user a sign-in link, call `signIn.magicLink`.

Browser code gets `AMPLIFY_BACKEND_URL` and `AMPLIFY_BACKEND_PUBLISHABLE_KEY` only when your framework inlines them as your app builds, such as with a Vite `define`. For more information, see the [client SDK README](https://github.com/aws-amplifygen3/amplifygen3/blob/main/packages/client-sdk/js/README.md) on GitHub.

```typescript
import { createClient } from '@aws-amplifygen3/backend-client-js'

const backend = createClient(import.meta.env.AMPLIFY_BACKEND_URL, import.meta.env.AMPLIFY_BACKEND_PUBLISHABLE_KEY)

const { error } = await backend.auth.signIn.magicLink({
  email,                                    // required
  callbackURL: `${location.origin}/notes`,  // the page to open after sign-in, on a trusted origin
})
```

When the user opens the link, the backend signs in that browser and opens `callbackURL`. The browser can be on a different device from the one where the user asked for the link. Each link works once. A used or expired link opens the backend’s "Sign-in failed" page. For more information about trusted origins, see [Auth configuration](backend-auth-config.md).

**Note**  
A deployed app uses token mode by default, because its backend is on another host. In token mode, the browser returns to the root page of the `callbackURL` origin first. Your root page must create the client. For more information, see [User sessions](backend-auth-sessions.md).

If the address has no account, opening the link creates one. To stop new sign-ups by link, set `"disableSignUp": true` in `magicLink`. The backend then sends no link to an address that has no account. It gives the same answer for every address, so the answer doesn’t reveal which addresses have an account. It also refuses a link that someone opens for an address with no account.

## Sign in with an email code
<a name="backend-auth-passwordless-code"></a>

To sign a user in with an email code, call two client SDK methods:

1. On your sign-in page, call `emailOtp.sendVerificationOtp` to email the user a code.

   ```typescript
   const { error } = await backend.auth.emailOtp.sendVerificationOtp({
     email,  // required
   })
   ```

1. When the user types the code into your app, call `signIn.emailOtp`.

   ```typescript
   const { data, error } = await backend.auth.signIn.emailOtp({
     email,  // required
     otp,    // required; the code from the email
   })
   ```

If the address has no account, signing in with a code creates one. To stop new sign-ups by code, set `"disableSignUp": true` in `emailOtp`. The backend then sends no code to an address that has no account, and refuses the sign-in.

## Users who haven’t verified their address
<a name="backend-auth-passwordless-unverified"></a>

**Important**  
An email sign-in removes the user’s other sign-in methods when their address isn’t verified. The user’s first sign-in with an email link or code verifies the address. It also deletes their password, their linked provider accounts, their passkeys and their two-step verification, and ends their sessions.

The user can set a new password with a password reset. To have users verify their address when they sign up with a password, turn on email verification. For more information, see [Email and password](backend-auth-password.md).

## Users with two-step verification
<a name="backend-auth-passwordless-two-step"></a>

For a user with two-step verification on, an email link or code replaces the password. The user still finishes the second step, with a code from their authenticator app or a backup code. Always pass a `callbackURL` on a trusted origin to `signIn.magicLink`. For a user with two-step verification, a link sent without one fails with `TWO_FACTOR_REQUIRES_PASSWORD`.

For how your app handles the second step after an email link or code, see [Two-step verification](backend-auth-two-step.md).

## Settings
<a name="backend-auth-passwordless-settings"></a>

Set these in `plugins` in the `auth` block. Times are in seconds. By default, a magic link lasts 5 minutes.


| Setting | Values | Default | 
| --- | --- | --- | 
|  `magicLink.enabled`  |  `true`, `false`  |  `false`  | 
|  `magicLink.expiresIn`  | 300 to 86400 |  `300`  | 
|  `magicLink.disableSignUp`  |  `true`, `false`  |  `false`  | 
|  `emailOtp.enabled`  |  `true`, `false`  |  `false`  | 
|  `emailOtp.expiresIn`  | 60 to 3600 |  `300`  | 
|  `emailOtp.otpLength`  | 4 to 10 digits |  `6`  | 
|  `emailOtp.disableSignUp`  |  `true`, `false`  |  `false`  | 

Each `disableSignUp` stops sign-ups for its own method only. To stop new sign-ups by password, link and code, set `disableSignUp` to `true` in `emailAndPassword`, `magicLink` and `emailOtp`.

## Errors
<a name="backend-auth-passwordless-errors"></a>

The client SDK returns each error’s code in `error.code`.


| Code | When | What to show | 
| --- | --- | --- | 
|  `error.status` is `404`, no code | A call for a method that you haven’t turned on | Turn on the method in `plugins`  | 
|  `new_user_signup_disabled`  | A user opens a link for an address with no account, with `magicLink.disableSignUp` on. It arrives as `?error=` on the `callbackURL` page, not in `error.code`  | That sign-up is by invitation | 
|  `INVALID_OTP`  | A sign-in by code for an address with no account, with `emailOtp.disableSignUp` on. The same code as a wrong code | That sign-up is by invitation, or ask the user to try again | 
|  `INVALID_OTP`, `OTP_EXPIRED`  | A wrong or expired code | Ask the user to try again or send a new code | 
|  `TOO_MANY_ATTEMPTS`  | A 4th try of a code, after 3 wrong ones | Send a new code | 
|  `INVALID_TOKEN`  | A used, expired or unknown link. The link opens the backend’s "Sign-in failed" page, and your page gets no error | A way to send a new link from your sign-in page | 
|  `INVALID_CALLBACK_URL`  |  `signIn.magicLink` with a `callbackURL` that isn’t on a trusted origin | Fix the URL or add its origin to `trustedOrigins`  | 
|  `VALIDATION_ERROR`  |  `signIn.magicLink` with an address that the backend doesn’t accept, such as `user@localhost`  | Ask for another address | 
|  `INVALID_EMAIL`  | A code request for an address that the backend doesn’t accept | Ask for another address | 
|  `error.status` is `500`  |  `signIn.magicLink` to an address at a domain reserved for testing, such as `example.com`, from the shared sender | Test with a real address, the local backend, or your own SMTP server | 
|  `over_request_rate_limit`  | Too many email requests for one address, or from one client | Try again in `error.retryAfter` seconds | 
|  `TWO_FACTOR_REQUIRED`  | A link sign-in in token mode for a user with two-step verification | Password sign-in or an email code | 
|  `TWO_FACTOR_REQUIRES_PASSWORD`  | A user whose only second step is the emailed code, or a link sent without a trusted `callbackURL`  | Password sign-in, then the emailed code | 
|  `EMAILED_SECOND_FACTOR_REFUSED`  | An emailed second-step code after an email sign-in | An authenticator code or a backup code | 

For the email limits, see [Quotas](quotas-chapter.md).