View a markdown version of this page

Social sign-in and OAuth - AWS Amplify

Social sign-in and OAuth

Amplify backend auth signs your users in with an account they already have at a provider. You configure providers in the auth block of amplify.jsonc: Amazon Cognito, Apple, Discord, Facebook, GitHub, Google, LinkedIn, Microsoft, Slack, Spotify and X (Twitter), and any other OAuth 2.0 or OpenID Connect provider.

Configure a social provider

The following steps add Google. To add another provider, follow the same steps and use its settings from Provider settings.

  1. Register your app with the provider, and note the client ID and client secret that the provider gives you. Add <backend URL>/auth/callback/google as a redirect URI for each backend that your users sign in on. Each backend has its own URL. For the local backend, use the endpoint that aws amplifygen3 local run prints, such as http://localhost:3001. For a deployed backend, use the URL that aws amplifygen3 backend info --env preview prints.

  2. Add the provider under socialProviders, with its clientId and clientSecret. The clientSecret takes only a ${env:NAME} reference, never the secret itself. Keep appName and emailAndPassword in the block, as in the example, so your users can still sign in with a password.

    // amplify.jsonc { "backend": { "auth": { "appName": "Acme Notes", "emailAndPassword": { "enabled": true }, "socialProviders": { "google": { "clientId": "123-abc.apps.googleusercontent.com", // required "clientSecret": "${env:GOOGLE_CLIENT_SECRET}" // required, a reference only } } } } }
  3. For the local backend, put the secret in .secrets/client_secrets.env, and run aws amplifygen3 local run again. Git ignores that file.

    GOOGLE_CLIENT_SECRET=your-client-secret
  4. For each environment, store the secret as a BACKEND variable with --secret, and then deploy. A deployment doesn’t send .secrets/client_secrets.env. If the variable is missing, is empty or was stored without --secret, the deployment fails with CONFIG_RETRIEVAL_FAILED.

    printf %s "$GOOGLE_CLIENT_SECRET" | aws amplifygen3 var set GOOGLE_CLIENT_SECRET --secret --scope BACKEND --env preview aws amplifygen3 deploy --env preview

For how the auth block and secrets work, see Auth configuration.

Sign users in with a provider

To sign a user in, call signIn.social with the provider’s key and a callbackURL on a trusted origin of your site.

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.signIn.social({ provider: 'google', // a key under socialProviders callbackURL: `${location.origin}/notes`, // the page to open after sign-in, on a trusted origin }) if (data?.url) location.assign(data.url) // send the browser to the provider

signIn.social doesn’t move the browser. It returns the provider’s sign-in URL in data.url, and your code sends the browser there in the same tab. The backend sends the browser back to callbackURL after the user signs in at the provider. For more information about trusted origins, see Auth configuration.

Note

A deployed app uses token mode by default, because its backend is on another host. In token mode, the backend first sends the browser to the root page of the callbackURL origin. Your root page must create the client. The client finishes the sign-in and then opens callbackURL. For more information, see User sessions.

The backend creates an account the first time a user signs in with a provider, even when emailAndPassword.disableSignUp is true. The managed sign-in page has a "Continue with" button for each provider that you configure.

Add a generic OAuth provider

To sign users in with an OAuth 2.0 or OpenID Connect provider that isn’t in Provider settings, such as your company’s sign-in service, add it to plugins.genericOAuth.providers. You can add up to 10 providers.

// amplify.jsonc { "backend": { "auth": { "appName": "Acme Notes", "emailAndPassword": { "enabled": true }, "plugins": { "genericOAuth": { "providers": [ { "providerId": "acme-sso", // required: 1 to 64 letters, digits, - and _; unique; part of the callback URL "name": "Acme SSO", // optional: the button label on the managed sign-in page; defaults to providerId "clientId": "notes-app", // required "clientSecret": "${env:ACME_SSO_CLIENT_SECRET}", // required, a reference only "discoveryUrl": "https://login.example.com/.well-known/openid-configuration", // or, without a discovery document: "authorizationUrl" and "tokenUrl", plus "userInfoUrl" for a provider that isn't OpenID Connect "scopes": ["openid", "email", "profile"] // optional: up to 50 } ] } } } } }

Every URL must start with https://. Register <backend URL>/auth/callback/<providerId> as the redirect URI. Store the secret as in steps 3 and 4 of Configure a social provider.

If the backend can’t read a generic provider’s discovery document, it leaves that provider out, and signing in with it fails with PROVIDER_NOT_FOUND. A deployment fails with CONFIG_RETRIEVAL_FAILED when a provider has neither discoveryUrl nor both endpoints. It also fails when two providers have the same providerId.

To sign a user in, call signIn.oauth2 with the providerId:

const { data, error } = await backend.auth.signIn.oauth2({ providerId: 'acme-sso', callbackURL: `${location.origin}/notes` }) if (data?.url) location.assign(data.url)

A user with linked accounts can sign in to one account with more than one provider, or with a provider and a password. Account linking is off by default. A provider sign-in fails with account_not_linked when linking is off and the email address already has an account. While linking is off, a signed-in user who links another provider goes to the provider, and then sees the backend’s "Sign-in failed" page with the error unable_to_link_account.

To turn linking on, add account.accountLinking to the auth block:

"account": { "accountLinking": { "enabled": true, // default false "trustedProviders": ["google"] // optional: up to 20 provider keys, providerIds or email-password } }

With linking on, a provider sign-in links to the existing user with the same email address when the existing user has verified their address. The provider must also report the address as verified, or be in trustedProviders. Otherwise the sign-in fails with account_not_linked.

A signed-in user can link another provider to their account. linkAccount returns the provider’s URL in data.url. Send the browser there, and it comes back to callbackURL, as in a sign-in. If the link fails, the browser opens the backend’s "Sign-in failed" page instead, with the error code in ?error=. To remove a provider, pass the account’s id from listAccounts() to unlinkAccount. unlinkAccount fails for a user’s only account.

const link = await backend.auth.linkAccount({ provider: 'github', callbackURL: `${location.origin}/settings` }) if (link.data?.url) location.assign(link.data.url) const { data } = await backend.auth.listAccounts() // each account: id, providerId (credential for email and password), accountId const github = data.accounts.find((account) => account.providerId === 'github') if (github) await backend.auth.unlinkAccount({ accountId: github.id }) // pass id, not accountId

Limit who can sign in

  • Your organization only – Set Microsoft’s tenantId to your tenant ID, or add your organization’s own OpenID Connect provider as a generic OAuth provider.

  • SAML identity providers – Auth signs users in through OAuth 2.0 and OpenID Connect providers.

  • Native apps – A sign-in goes through the provider’s page in a browser. The client SDK has no call for an ID token that a native app gets from the provider.

  • Email domains – No setting limits sign-in to some email domains, and no provider has its own switch that stops new sign-ups.

Provider settings

Each provider in socialProviders takes a clientId and a clientSecret. Three take more:

Provider Key Other settings

Amazon Cognito

cognito

domain, region and userPoolId, all required

Apple

apple

clientId is your Services ID. Optional appBundleIdentifier, your iOS or macOS app’s bundle ID

Microsoft

microsoft

Optional tenantId: a tenant ID, common, organizations or consumers. Default common, any Microsoft account

Discord, Facebook, GitHub, Google, LinkedIn, Slack, Spotify, X (Twitter)

discord, facebook, github, google, linkedin, slack, spotify, twitter

None

Errors

Code When What to do

missing_client_secret

local run finds no value for a referenced secret

Add the name and its value to .secrets/client_secrets.env

PROVIDER_NOT_FOUND

The provider isn’t configured

Add it to the auth block, then run local run or deploy

TOKEN_MODE_CALLBACK_REQUIRED

In token mode, callbackURL isn’t on your app’s own URL, a custom domain or an exact trustedOrigins entry

Use a callbackURL on one of them

account_not_linked

Linking is off, or the sign-in can’t be linked, and the email address already has an account

Ask the user to sign in the way they did before, or turn on linking

TOKEN_MODE_RESUME_REQUIRED

A link in token mode from an older @aws-amplifygen3/auth-js

Update it and @aws-amplifygen3/backend-client-js

SESSION_NOT_FRESH

Unlinking with a session older than session.freshAge, 1 day by default

Ask the user to sign in again

FAILED_TO_UNLINK_LAST_ACCOUNT

Unlinking the user’s only account

Link another account first

When linking a provider fails, the browser opens the backend’s "Sign-in failed" page with one of the following codes in ?error=. These codes never appear in error.code from linkAccount.

Code When What to do

email_does_not_match

The provider account has another email address

Link a provider account with the user’s address

unable_to_link_account

Linking is off, or the provider doesn’t report the address as verified and isn’t in trustedProviders

Turn on linking, or add the provider to trustedProviders

account_already_linked_to_different_user

Another user has this provider account

Sign in as that user

For every amplify.jsonc setting, see Configuration reference.