Email and password
Your users can create an account and sign in with an email address and a password. Email and password sign-in is on by default. You configure it in the emailAndPassword object of the auth block in amplify.jsonc.
Turn on email and password sign-in
An auth block that sets anything replaces the default settings, so password sign-in stays on only if the block includes emailAndPassword with "enabled": true, as in the following example. That includes a block that you add only for a social provider. A block without it turns password sign-in off: sign-up then fails with EMAIL_PASSWORD_SIGN_UP_DISABLED, and sign-in with EMAIL_PASSWORD_DISABLED. An empty block, "auth": {}, uses the default settings. For how the auth block works and how to apply your changes, see Auth configuration.
appName is the name your users see in auth emails and on the managed sign-in page.
// amplify.jsonc { "backend": { "auth": { "appName": "Acme Notes", "emailAndPassword": { "enabled": true, "minPasswordLength": 12 } } } }
Sign users up
To sign a user up, call signUp.email.
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 { data, error } = await backend.auth.signUp.email({ email, // required password, // required; 8 to 128 characters by default name, // optional; filled in from the email address when left out callbackURL, // optional; where the verification link lands, on a trusted origin }) // data: { user, session }; session is null when the user must verify or sign in first
With the default settings, signUp.email signs the user in at once and sets emailVerified to false. A sign-up for an address that already has an account fails. To keep sign-up from revealing which addresses have an account, see Hide whether an address has an account. To make users sign in after they sign up, set "autoSignIn": false. To stop new sign-ups with a password, set "disableSignUp": true. Email links, email codes and social providers still create accounts unless you turn off their own sign-ups. Existing users can still sign in, and you can still create accounts yourself. For more information, see Manage users.
Sign users in
To sign a user in, call signIn.email.
const { data, error } = await backend.auth.signIn.email({ email, // required password, // required })
The client SDK keeps the session and sends the user’s token with each request. For more information, see User sessions.
Sign users out
To sign a user out, call signOut.
await backend.auth.signOut()
Verify email addresses
To make users confirm their address before they can sign in, set "requireEmailVerification": true in emailAndPassword. signUp.email then sends a verification email and returns no session. To bring the user back to your site, pass a callbackURL on a trusted origin to signUp.email and resendVerificationEmail. Without a callbackURL, the link opens the backend’s own confirmation page. Opening the link verifies the address but doesn’t sign the user in.
signIn.email fails with EMAIL_NOT_VERIFIED for a user who hasn’t verified their address, and the backend emails a new link. The link opens the root of your site when the sign-in came from a trusted origin. Otherwise it opens the backend’s own confirmation page. A resendVerificationEmail call within a minute of the refused sign-in sends nothing. An email change shares the same one-minute wait. To send a link that returns to a page that you choose, set "emailVerification": { "sendOnSignIn": false }. Then call resendVerificationEmail with a callbackURL when sign-in fails with EMAIL_NOT_VERIFIED.
await backend.auth.resendVerificationEmail({ email, // required callbackURL, // optional; on a trusted origin })
The backend sends at most one password reset link, and one resent verification email, to an address about every minute. A request within that minute succeeds, but no new email goes out, and the link in the earlier email still works. If the earlier email failed to send, the next request sends one at once.
rateLimit is on by default on a deployed backend. When it’s on, your app can make at most 5 requests for auth emails to one address in a one-minute window. Until the window ends, the backend refuses more with 429
over_request_rate_limit, and the SDK puts the wait in error.retryAfter. A request counts even when no email goes out. The verification email that sign-up or sign-in sends, and the account deletion email, don’t count. For more information about other limits, see Quotas. On the local backend, the emails arrive in Mailpit, unless the root auth block sets your own SMTP server.
Hide whether an address has an account
With the defaults, a sign-up for a registered address fails with USER_ALREADY_EXISTS_USE_ANOTHER_EMAIL. Set "requireEmailVerification": true or "autoSignIn": false, and a sign-up for a registered address gets the same response as a sign-up for a new address. The backend doesn’t create an account or send an email for it. Sign-in and password reset return the same response for any address.
Reset a password
-
On your forgot-password page, call
requestPasswordReset. The backend emails the user a "Reset your password" link. The link opens your backend first, which sends the user on toredirectTowith?token=added.await backend.auth.requestPasswordReset({ email, // required redirectTo, // your reset page, on a trusted origin; without it, the link opens a page with no form }) -
On the
redirectTopage, read the token and set the new password. When the reset completes, the backend ends all of the user’s sessions.const token = new URLSearchParams(location.search).get('token') ?? '' const { error } = await backend.auth.resetPassword({ token, newPassword })
Change a password
To change a signed-in user’s password, call changePassword.
const { error } = await backend.auth.changePassword({ currentPassword, // required; a wrong one returns an error newPassword, // required revokeOtherSessions, // optional; true signs the user out on their other devices })
Settings
Set these in emailAndPassword and emailVerification in the auth block. Times are in seconds.
| Setting | Values | Default |
|---|---|---|
|
|
|
|
|
|
6 to 20, and no more than |
8 |
|
|
8 to 128 |
128 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
300 to 86400 |
3600 (1 hour) |
|
|
|
Same as |
|
|
|
|
|
|
300 to 86400 |
3600 (1 hour) |
signIn.email doesn’t check minPasswordLength. Users whose passwords are shorter than a new minimum can still sign in. sendOnSignUp and sendOnSignIn can’t both be false when emailAndPassword.enabled and requireEmailVerification are both true.
Errors
The client SDK returns each error’s code in error.code.
| Code | When | What to show |
|---|---|---|
|
|
Sign-up or reset with a password under |
The minimum length |
|
|
Sign-up for a registered address, with the defaults |
A link to sign in or reset the password |
|
|
Sign-up with |
That sign-up is by invitation |
|
|
Sign-in with an |
Your other sign-in methods |
|
|
|
Fix the URL or add its origin to |
|
|
|
Fix the URL or add its origin to |
|
|
Sign-in with a wrong email address or password |
"Wrong email or password" |
|
|
Sign-in before the user opens the verification link |
Check your inbox, with a resend button |
|
|
Too many requests from one client, or too many requests for auth emails to one address |
Try again in |
|
|
The backend can’t be reached |
"Can’t reach the server" |
An expired verification link, and a reset link that was used or has expired, open the backend’s "Sign-in failed" page. A verification link that was already used still opens your callbackURL.