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
Add magicLink for email links, emailOtp for email codes, or both, to plugins, each with "enabled": true, next to your other auth settings:
// 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.
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. 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.
Users of the managed sign-in page 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
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
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.
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.
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
To sign a user in with an email code, call two client SDK methods:
-
On your sign-in page, call
emailOtp.sendVerificationOtpto email the user a code.const { error } = await backend.auth.emailOtp.sendVerificationOtp({ email, // required }) -
When the user types the code into your app, call
signIn.emailOtp.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
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.
Users with two-step verification
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.
Settings
Set these in plugins in the auth block. Times are in seconds. By default, a magic link lasts 5 minutes.
| Setting | Values | Default |
|---|---|---|
|
|
|
|
|
|
300 to 86400 |
|
|
|
|
|
|
|
|
|
|
|
60 to 3600 |
|
|
|
4 to 10 digits |
|
|
|
|
|
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
The client SDK returns each error’s code in error.code.
| Code | When | What to show |
|---|---|---|
|
|
A call for a method that you haven’t turned on |
Turn on the method in |
|
|
A user opens a link for an address with no account, with |
That sign-up is by invitation |
|
|
A sign-in by code for an address with no account, with |
That sign-up is by invitation, or ask the user to try again |
|
|
A wrong or expired code |
Ask the user to try again or send a new code |
|
|
A 4th try of a code, after 3 wrong ones |
Send a new code |
|
|
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 |
|
|
|
Fix the URL or add its origin to |
|
|
|
Ask for another address |
|
|
A code request for an address that the backend doesn’t accept |
Ask for another address |
|
|
|
Test with a real address, the local backend, or your own SMTP server |
|
|
Too many email requests for one address, or from one client |
Try again in |
|
|
A link sign-in in token mode for a user with two-step verification |
Password sign-in or an email code |
|
|
A user whose only second step is the emailed code, or a link sent without a trusted |
Password sign-in, then the emailed code |
|
|
An emailed second-step code after an email sign-in |
An authenticator code or a backup code |
For the email limits, see Quotas.