Google and GitHub
methods: [
google(process.env.GOOGLE_CLIENT_ID, process.env.GOOGLE_CLIENT_SECRET),
github(process.env.GITHUB_CLIENT_ID, process.env.GITHUB_CLIENT_SECRET),
]A login button is a plain link:
<a href="/auth/google?next=/app">Continue with Google</a>
<a href="/auth/github">Continue with GitHub</a>The flow:
/auth/googlestores the flow state in an encrypted cookie and redirects to Google.- Google redirects back to
/auth/google/callback. - The package checks the answer, finds or creates the user, sets the session cookie and redirects to
next. - On an error it redirects to
pages.error?error=<code>.
Set up Google
- Open Google Cloud console → Google Auth Platform. Make one project per app, so the consent screen shows that app's name and logo.
- Under Branding, set the app name, logo and support e-mail.
- Under Clients, create a Web application client.
- Add the redirect URI that
bunx kaarel-auth doctorprints:https://<your host>/auth/google/callback. For local development also addhttp://localhost:3000/auth/google/callback. - Under Audience, publish the app ("In production"). In "Testing", only the listed test users can sign in.
- Put the client ID and secret in
GOOGLE_CLIENT_IDandGOOGLE_CLIENT_SECRET.
Set up GitHub
- Open GitHub → Settings → Developer settings → OAuth Apps → New OAuth App.
- Set Authorization callback URL to
https://<your host>/auth/github/callback. - Create a client secret. Put both values in
GITHUB_CLIENT_IDandGITHUB_CLIENT_SECRET.
Which user signs in
| Case | Result |
|---|---|
| The Google/GitHub account is already linked | That user signs in. |
| A user has the same e-mail, verified | The account is linked to that user, who signs in. |
| A user has the same e-mail, never verified | The provider proves who owns the e-mail. Everything that gives access to that user is removed: password, sessions, API tokens, other linked accounts, 2FA. Then the account is linked. This stops someone who signed up with another person's e-mail. |
| No user has the e-mail | A new user is created, if access allows sign-up. |
| The provider says the e-mail is not verified | oauth_email_unverified. GitHub uses only the primary e-mail. |
Errors from the callback: oauth_denied (the user cancelled), oauth_failed, oauth_email_unverified, signup_closed, access_denied, method_disabled, rate_limited.
Link an account to the signed-in user
On the settings page:
<a href="/auth/google?link=1&next=/settings">Connect Google</a>After the link, the user returns to next, and gets an e-mail that a provider was connected. On a problem they return to next?error=<code>:
account_linked_elsewhere: that Google account belongs to another user.reauth_required: the session is older than 15 minutes. Ask for the password withauth.account.reauthenticate({ password }), or have the user sign in again, then retry.email_not_verified: the user's own e-mail is not verified yet.
const { hasPassword, accounts } = await auth.account.linked();
await auth.account.unlink({ provider: "google" }); // error last_sign_in_method if it is the only way inWhat the package checks
state(CSRF), PKCE with S256, and for Google anonce.- The flow cookie is encrypted with AES-256-GCM and works for 10 minutes, in the browser that started the login.
- The Google ID token comes straight from Google's token endpoint over TLS, with the client secret. OIDC Core 3.1.3.7 allows TLS server validation in place of the signature check in this case. The package still checks
iss,aud,exp,nonceandemail_verified.