View a markdown version of this page

Local auth and emails - AWS Amplify

Local auth and emails

The local AWS Amplify backend runs auth on your computer with the backend.auth settings in your amplify.jsonc file, the same settings that a deployment sends to an environment. Its publishable key is always hpk_v1.local. It sends auth emails to Mailpit on your computer.

Call the local backend

aws amplifygen3 local run starts your dev server with the local backend’s URL in AMPLIFY_BACKEND_URL and the key in AMPLIFY_BACKEND_PUBLISHABLE_KEY. A deployment sets the same variables. For a script or a curl call, run the following command to read the URL from amplify.local.json:

API=$(jq -r '.local.endpoint' amplify.local.json)

Send hpk_v1.local as the apikey header on every call, signed out or with a user’s token after sign-in. Run the following command to call the backend as a signed-out visitor:

curl -H "apikey: hpk_v1.local" "$API/rest/notes"

A call with any other apikey value gets 401.

To call the backend as service_role from server code or a script, sign the request with AWS Signature Version 4 (SigV4). service_role has full access. On the local backend, a signature made with any credentials, Region and service name works. Run the following command to send a signed request:

curl -s --aws-sigv4 "aws:amz:us-east-1:local" --user "local:local" "$API/rest/notes"

The client SDK signs local requests with placeholder credentials. Your server code needs no AWS credentials locally. For more information about signed requests on a deployed backend, see API keys and roles.

Sign in from your dev server

aws amplifygen3 local run passes the origins of the dev server that it starts to the local backend as trusted origins: http and https, on localhost and 127.0.0.1. The CLI passes them when it starts the local backend, and it doesn’t write them to amplify.jsonc. A deployment doesn’t send them. When you don’t set passkey.origins, the local backend also accepts passkeys from the dev server origins that are on the rpId host.

If you start a dev server another way, add its origin, such as http://localhost:5173, to trustedOrigins in backend.auth. Until you add it, browser calls from that dev server fail with Failed to fetch. The output of aws amplifygen3 local logs has a warning that starts the local backend does not trust this page origin. For more information, see Auth configuration.

Create local users

Local users are separate from the users of every environment. A deployment copies no users from your computer.

To create a test user, run the following command. It reads the password from standard input:

printf %s "$TEST_PASSWORD" | aws amplifygen3 local backend user create --email test-owner-1@example.com --email-verified

aws amplifygen3 local backend user also has list, get, edit and delete. To delete the test user when you’re done, run the following command:

aws amplifygen3 local backend user delete --email test-owner-1@example.com --yes

You can also create test users on the Authentication screen of the local console. For more information about each command, see Manage users.

Read auth emails

The local backend sends its auth emails to Mailpit, a mail catcher that aws amplifygen3 local run starts on your computer. These emails include verification emails, password reset links, and sign-in links and codes. Unless you set your own SMTP server, no auth email reaches a real inbox.

To read them, open the mail.ui address that aws amplifygen3 local status prints. Read the address each time, because the port can change at each start, and port 8025 can belong to another project’s mail catcher. Mailpit keeps emails in memory and loses them when it restarts.

Each email comes from your appName at noreply@amplifygen3.local. The following image shows Mailpit with two verification emails and a password reset email:

Mailpit’s inbox with two Verify your email messages and one Reset your password message, each from Notes demo

Open an email to see its link. The following image shows a verification email:

A verification email in Mailpit, from Notes demo at noreply@amplifygen3.local, with a Verify email link

A sign-up sends a verification email only when emailAndPassword.requireEmailVerification or emailVerification.sendOnSignUp is true. Neither is set by default. For more information about the emails and their templates, see Auth emails.

Send through your own SMTP server

If the root backend.auth block sets platform.customEmail.smtp, the local backend sends auth emails through that server, with your templates in email-templates/. The local backend reads only the root block and ignores each env.<name>.backend.auth block.

The server must accept TLS, either STARTTLS or implicit TLS on port 465, and have a certificate from a public certificate authority. A server with a self-signed certificate fails. Mailpit can’t be that server. aws amplifygen3 local run stops with invalid_auth_config when host is an IP address, a name with no dot, or a name that ends in .local or .localhost. To check a template before you send it, run aws amplifygen3 templates validate.

Store client secrets

Each secret in backend.auth is a ${env:NAME} reference. Secrets include a social provider’s clientSecret and the SMTP password. For the local backend, put a NAME=value line for each reference in .secrets/client_secrets.env. For example, the reference ${env:GOOGLE_CLIENT_SECRET} needs the line GOOGLE_CLIENT_SECRET=<value>. Git ignores the .secrets/ folder. If a referenced name is missing, aws amplifygen3 local run stops with missing_client_secret. For more information about the secrets of a deployed environment, see Auth configuration.

Apply auth changes

The local backend reads its auth settings when it starts. After you change any of the following files, press Ctrl+C in the local run terminal, and then run aws amplifygen3 local run again:

  • The backend.auth block in amplify.jsonc

  • .secrets/client_secrets.env

  • A file in email-templates/

The command restarts the backend with the new settings. Your local data and users stay. For more information, see Auth configuration.

How local auth differs from a deployed backend

Behavior Local backend Deployed backend

Publishable key

hpk_v1.local

A key that each deployment sets in AMPLIFY_BACKEND_PUBLISHABLE_KEY

Signed requests (service_role)

A signature from any credentials

A signature from an identity in the backend’s AWS account with amplifygen3:Connect

Users

Your local users only

Each environment’s own users

Rate limits on sign-in and other auth requests

Off by default

On

A user’s token after sign-out

Works until it expires

Gets 401 within 30 seconds

Session cookie name

amplifygen3.session_token

__Host-amplifygen3.session_token (by default). With advanced.crossSubDomainCookies, the name has no __Host- prefix

defaultCookieAttributes.sameSite set to None

Browsers refuse the cookie, because the local backend serves over http

Browsers accept the cookie over https

A refused advanced.crossSubDomainCookies.domain value

The local backend starts, because it doesn’t check the value

The backend doesn’t start

Auth settings

The root backend.auth block

The root block, or the environment’s env.<name>.backend.auth block

Auth emails

Mailpit, or your SMTP server

A shared no-reply sender, or your SMTP server

Troubleshooting

Problem Cause What to do

401 with invalid_credential

The call sent no apikey, or a value other than hpk_v1.local

Send apikey: hpk_v1.local on every call that isn’t signed

Failed to fetch in the browser

The page’s origin isn’t in the trusted origins of the local backend

Start the dev server with aws amplifygen3 local run, or add its origin to trustedOrigins

missing_client_secret

.secrets/client_secrets.env has no line for a ${env:NAME} reference

Add the line, then run aws amplifygen3 local run

invalid_auth_config

A backend.auth key or value breaks a rule, such as an SMTP host with no dot. The error names the key

Fix the key, then run aws amplifygen3 local run

config_not_applied

Auth didn’t start with the edited settings. The local backend kept the previous settings

Fix what the error quotes, then run aws amplifygen3 local run

token_mode_unsupported on sign-in

The client SDK is in token mode, and the local backend runs an image from before token mode

Update the AWS Amplify CLI, then run aws amplifygen3 local run --rebuild. Your data stays

A warning that ends local auth email will not be captured

Mailpit didn’t start

Run aws amplifygen3 local stop, then aws amplifygen3 local run

No verification email after sign-up

Email verification is off by default

Set emailAndPassword.requireEmailVerification to true