---
title: Google and GitHub
description: Social login, provider setup and account linking.
---

# Google and GitHub

```ts
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:

```tsx
<a href="/auth/google?next=/app">Continue with Google</a>
<a href="/auth/github">Continue with GitHub</a>
```

The flow:

1. `/auth/google` stores the flow state in an encrypted cookie and redirects to Google.
2. Google redirects back to `/auth/google/callback`.
3. The package checks the answer, finds or creates the user, sets the session cookie and redirects to `next`.
4. On an error it redirects to `pages.error?error=<code>`.

## Set up Google

1. Open [Google Cloud console → Google Auth Platform](https://console.cloud.google.com/auth/overview). Make one project per app, so the consent screen shows that app's name and logo.
2. Under **Branding**, set the app name, logo and support e-mail.
3. Under **Clients**, create a **Web application** client.
4. Add the redirect URI that `bunx kaarel-auth doctor` prints: `https://<your host>/auth/google/callback`. For local development also add `http://localhost:3000/auth/google/callback`.
5. Under **Audience**, publish the app ("In production"). In "Testing", only the listed test users can sign in.
6. Put the client ID and secret in `GOOGLE_CLIENT_ID` and `GOOGLE_CLIENT_SECRET`.

## Set up GitHub

1. Open **GitHub → Settings → Developer settings → OAuth Apps → New OAuth App**.
2. Set **Authorization callback URL** to `https://<your host>/auth/github/callback`.
3. Create a client secret. Put both values in `GITHUB_CLIENT_ID` and `GITHUB_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:

```tsx
<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 with `auth.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.

```ts
const { hasPassword, accounts } = await auth.account.linked();
await auth.account.unlink({ provider: "google" }); // error last_sign_in_method if it is the only way in
```

## What the package checks

- `state` (CSRF), PKCE with S256, and for Google a `nonce`.
- 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`, `nonce` and `email_verified`.
