# Add Google Login to a Next.js App with NextAuth.js

Yes. An agent adds the Google provider to NextAuth.js, wires the env vars and a sign-in button, settles how it meets an existing email account, and opens a PR.

[Run this on Rush](https://cloud.getrush.ai/run/add-google-login/next-auth/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent reads the existing sign-in before it adds one. In a Next.js app laid out like shadcn-ui/taxonomy, `lib/auth.ts` exports `authOptions` with the `PrismaAdapter`, a `jwt` session strategy, `/login` as the sign-in page, a `GitHubProvider` from `GITHUB_CLIENT_ID` and `GITHUB_CLIENT_SECRET`, and an `EmailProvider` whose `sendVerificationRequest` sends a Postmark template chosen by whether the user's `emailVerified` is set. The `jwt` callback looks the user up by email and copies `id`, `name`, `email` and `image` into the token. `package.json` pins `next-auth` at `4.22.1`, `env.mjs` validates every secret with `@t3-oss/env-nextjs` and zod, and `components/user-auth-form.tsx` renders the email form plus one outline button that calls `signIn("github")`, used by both the login and register pages. `prisma/schema.prisma` has the adapter's `Account` model with `@@unique([provider, providerAccountId])` on a MySQL datasource.

It adds `GoogleProvider` from `next-auth/providers/google` to the `providers` array with `GOOGLE_CLIENT_ID` and `GOOGLE_CLIENT_SECRET`, adds both to `env.mjs` as required strings and to `.env.example` with a comment naming the redirect URI Google needs, `/api/auth/callback/google` on the app's origin. No schema change is needed: the adapter stores the Google account in the same `Account` table under `provider: "google"`.

The decision that matters is what happens when someone who already signed in by email link now clicks Google with the same address. NextAuth.js refuses that by default and sends the user back with `OAuthAccountNotLinked`, because trusting a provider's email claim can let one account take over another. The agent reads Google's profile, where `email_verified` is a field, and turns on `allowDangerousEmailAccountLinking` for the Google provider only, with a `signIn` callback that refuses a Google profile whose `email_verified` is false. It also sets `emailVerified` on the user when a verified Google sign-in creates them, so the Postmark template choice in the email provider keeps working for a user who later signs in by link. The pull request states this choice plainly so a reviewer can reject it and keep the default.

In `user-auth-form.tsx` it adds a second outline button beside GitHub with its own loading state and `signIn("google", { callbackUrl })` using the same `from` parameter the email form reads, and a `google` glyph in `components/icons.tsx`, which has GitHub but no Google mark today. It runs `yarn lint` and `tsc --noEmit`, then starts the dev server in its sandbox with placeholder Google credentials to confirm `/api/auth/providers` lists `google` and the login page renders both buttons; the full OAuth round trip needs the reader's Google client, so the pull request ends with the console steps to create one.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Add Google login with NextAuth.js. Read package.json, lib/auth.ts, env.mjs, .env.example, components/user-auth-form.tsx, components/icons.tsx, app/(auth)/login/page.tsx, app/(auth)/register/page.tsx and prisma/schema.prisma first. Add GoogleProvider with GOOGLE_CLIENT_ID and GOOGLE_CLIENT_SECRET, validated in env.mjs and documented in .env.example with the callback URL. Decide how a Google sign-in meets an existing email-only user: enable allowDangerousEmailAccountLinking on the Google provider only, refuse profiles whose email_verified is false in a signIn callback, and set emailVerified when a verified Google sign-in creates the user. Add a Google button with its own loading state and icon beside the GitHub one. Run yarn lint and tsc --noEmit, start the dev server with placeholder credentials to confirm /api/auth/providers lists google and the login page renders, and open a pull request that explains the linking decision and the steps to create the Google OAuth client.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

What comes back is a pull request that changes `lib/auth.ts`, `env.mjs`, `.env.example`, `user-auth-form.tsx` and `icons.tsx`, with no migration. The description quotes the provider block and the `signIn` callback, explains the account-linking decision in a paragraph, and lists the Google Cloud console steps: the OAuth consent screen, the web client, and the exact redirect URI for local and production origins.

The run's record keeps the lint and type-check output and the `/api/auth/providers` response from the sandbox, so a reviewer sees the provider registered rather than assumes it.

Before merging, a reviewer decides the linking question with the team: keep the agent's verified-email linking, or remove `allowDangerousEmailAccountLinking` and tell a user with an existing email account to sign in by link first. The pull request works either way; the second choice is one line removed.

## Run it on every Linear issue labelled auth

A team makes this a rule. In the Project for the repository, an automation with the prompt above and a `linear` trigger on `linear.issue.labeled` for the label `auth` starts a run when someone files "add Google sign-in" or "users on Google Workspace cannot use magic links". The trigger adds the issue after the prompt, so the run reads whether the issue wants a specific Workspace domain restricted and adds that to the `signIn` callback.

Each run opens its own pull request on its own branch and links the issue. A repository that already has the Google provider gets a description saying so and a pull request only for the part the issue asks for, such as the domain restriction or the linking rule.

## Why run it on Rush?

Adding a provider is ten lines; deciding what happens when a Google email matches an existing account is the part that gets skipped, and the default behaviour, a cryptic `OAuthAccountNotLinked` redirect, is what users hit first. The agent reads the adapter, the provider's profile shape and the existing email flow, makes the decision explicit in code and in the description, and checks the provider is registered in a running app in its sandbox.

The pull request is one provider, one callback and one button, so review is a single question for the team. A company with several Next.js apps on NextAuth.js runs them all at once, each in its own sandbox with its own pull request.

## Frequently asked questions

### Why is email account linking called dangerous?

Because it trusts a provider's email claim to attach a new sign-in to an existing user. Google verifies the address and says so in `email_verified`, so the agent enables linking for Google alone and refuses an unverified profile in the `signIn` callback. The GitHub provider keeps the default.

### Does this need a database migration?

No. The adapter's `Account` table already has `provider` and `providerAccountId` with a unique constraint across both, and a Google account is one more row there. The user row is shared, which is what makes linking possible.

### Can sign-in be limited to one Google Workspace domain?

Yes. Google's profile carries `hd`, the hosted domain, and the `signIn` callback can refuse any other value. The agent leaves that out unless the prompt or the issue asks for it, and the pull request shows the two-line check.

### What about the session callback and user id?

Unchanged. The `jwt` callback already looks the user up by email on every token refresh and the `session` callback copies the id, so a Google user gets the same `session.user.id` the rest of the app, including the Stripe routes, relies on.

## More NextAuth.js jobs

- [Add Role-Based Access Control to a NextAuth.js App](https://getrush.ai/run/add-rbac/next-auth/)
- [Add Refresh Token Rotation to a NextAuth.js App](https://getrush.ai/run/add-refresh-tokens/next-auth/)
- [Add Sign in With GitHub to a Next.js App Using Auth.js](https://getrush.ai/run/add-github-login/next-auth/)

## The same job on other stacks

- [Add Google Login to an Express App with Passport](https://getrush.ai/run/add-google-login/passport/)
- [Add Google Login to a Streamlit App](https://getrush.ai/run/add-google-login/streamlit/)
- [Add Google Login to an AdonisJS App](https://getrush.ai/run/add-google-login/adonisjs/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
