View a markdown version of this page

Email and password - AWS Amplify

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 on GitHub.

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

  1. 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 to redirectTo with ?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 })
  2. On the redirectTo page, 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

emailAndPassword.enabled

true, false

true in the default settings

emailAndPassword.minPasswordLength

6 to 20, and no more than maxPasswordLength

8

emailAndPassword.maxPasswordLength

8 to 128

128

emailAndPassword.autoSignIn

true, false

true

emailAndPassword.disableSignUp

true, false

false

emailAndPassword.requireEmailVerification

true, false

false

emailAndPassword.resetPasswordTokenExpiresIn

300 to 86400

3600 (1 hour)

emailVerification.sendOnSignUp

true, false

Same as requireEmailVerification

emailVerification.sendOnSignIn

true, false; true needs requireEmailVerification

true when requireEmailVerification is true

emailVerification.expiresIn

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

PASSWORD_TOO_SHORT

Sign-up or reset with a password under minPasswordLength

The minimum length

USER_ALREADY_EXISTS_USE_ANOTHER_EMAIL

Sign-up for a registered address, with the defaults

A link to sign in or reset the password

EMAIL_PASSWORD_SIGN_UP_DISABLED

Sign-up with disableSignUp on, or with an auth block that leaves out emailAndPassword

That sign-up is by invitation

EMAIL_PASSWORD_DISABLED

Sign-in with an auth block that leaves out emailAndPassword

Your other sign-in methods

INVALID_CALLBACK_URL

callbackURL isn’t on a trusted origin; no account is created

Fix the URL or add its origin to trustedOrigins

INVALID_REDIRECT_URL

redirectTo on requestPasswordReset isn’t on a trusted origin

Fix the URL or add its origin to trustedOrigins

INVALID_EMAIL_OR_PASSWORD

Sign-in with a wrong email address or password

"Wrong email or password"

EMAIL_NOT_VERIFIED

Sign-in before the user opens the verification link

Check your inbox, with a resend button

over_request_rate_limit

Too many requests from one client, or too many requests for auth emails to one address

Try again in error.retryAfter seconds

error.status is 0

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.