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.
-
Register your app with the provider, and note the client ID and client secret that the provider gives you. Add
<backend URL>/auth/callback/googleas a redirect URI for each backend that your users sign in on. Each backend has its own URL. For the local backend, use theendpointthataws amplifygen3 local runprints, such ashttp://localhost:3001. For a deployed backend, use theURLthataws amplifygen3 backend info --env previewprints. -
Add the provider under
socialProviders, with itsclientIdandclientSecret. TheclientSecrettakes only a${env:NAME}reference, never the secret itself. KeepappNameandemailAndPasswordin 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 } } } } } -
For the local backend, put the secret in
.secrets/client_secrets.env, and runaws amplifygen3 local runagain. Git ignores that file.GOOGLE_CLIENT_SECRET=your-client-secret -
For each environment, store the secret as a
BACKENDvariable 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 withCONFIG_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
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.
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)
Link accounts
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
tenantIdto 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 |
|
|
|
Apple |
|
|
|
Microsoft |
|
Optional |
|
Discord, Facebook, GitHub, Google, LinkedIn, Slack, Spotify, X (Twitter) |
|
None |
Errors
| Code | When | What to do |
|---|---|---|
|
|
|
Add the name and its value to |
|
|
The provider isn’t configured |
Add it to the |
|
|
In token mode, |
Use a |
|
|
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 |
|
|
A link in token mode from an older |
Update it and |
|
|
Unlinking with a session older than |
Ask the user to sign in again |
|
|
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 |
|---|---|---|
|
|
The provider account has another email address |
Link a provider account with the user’s address |
|
|
Linking is off, or the provider doesn’t report the address as verified and isn’t in |
Turn on linking, or add the provider to |
|
|
Another user has this provider account |
Sign in as that user |
For every amplify.jsonc setting, see Configuration reference.