# Add Sign in With GitHub to a Next.js App Using Auth.js

Yes. An agent adds the GitHub provider to Auth.js, creates or matches the user row by email, keeps the JWT's id as the database id, adds the button and opens a PR.

[Run this on Rush](https://cloud.getrush.ai/run/add-github-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 starts with how sign-in works now. In a Next.js app laid out like vercel/chatbot, `package.json` pins `next-auth` at 5.0.0-beta.25 and `app/(auth)/auth.ts` calls `NextAuth` with two `Credentials` providers: one that takes email and password, loads the row with `getUser(email)` and compares with `bcrypt-ts`, running the same compare against `DUMMY_PASSWORD` when there is no user or no password so a missing account takes as long as a wrong password; and one with `id: "guest"` that calls `createGuestUser()`, which inserts a `guest-<timestamp>` email with a random hashed password. The `jwt` callback copies `user.id` and `user.type` into the token and the `session` callback copies them back. `app/(auth)/auth.config.ts` sets `basePath: "/api/auth"`, `pages.signIn` to `/login` and `trustHost: true`, and `proxy.ts` sends anyone without a token to `/api/auth/guest`, lets `/api/auth/*` through, and bounces a signed-in non-guest away from `/login` and `/register`.

The database is closer to ready than it looks. `lib/db/schema.ts` gives the `User` table `email` as a required `varchar(64)`, `password` as a nullable `varchar(64)`, and `name`, `image`, `emailVerified` and `isAnonymous` columns that nothing writes today; there is no Auth.js `account` table and `lib/db/migrations/0000_initial.sql` puts no unique index on `email`. Since the app uses the JWT session strategy with no adapter, the agent keeps it that way: it adds `GitHub` from `next-auth/providers/github` to `providers`, which Auth.js configures from `AUTH_GITHUB_ID` and `AUTH_GITHUB_SECRET` without code, and documents both names in `.env.example` beside `AUTH_SECRET`. A `signIn` callback runs for `account.provider === "github"`: it looks the profile's email up with `getUser`, and when no row exists inserts one through a new `createOAuthUser(email, name, image)` in `lib/db/queries.ts` with `password` left null and `emailVerified` true. A row with no password already cannot sign in through the credentials form, because `authorize` returns null on `!user.password`, so an account created from GitHub stays GitHub-only until the person sets a password.

The `jwt` callback is the part that is easy to get wrong. On a GitHub sign-in the `user` Auth.js hands it carries GitHub's numeric id, not the row's uuid, and every query in `lib/db/queries.ts` keys chats on `userId` as a uuid. So when `account.provider` is `github` the callback reads the row by `token.email` and sets `token.id` to its `id` and `token.type` to `regular`; the `session` callback then works unchanged. The login and register pages gain a "Continue with GitHub" button under the `AuthForm` from `components/chat/auth-form.tsx`, submitting a form whose server action calls `signIn("github", { redirectTo: "/" })`, and `proxy.ts` needs no change because `/api/auth/*` already passes and the post-login redirect for non-guests already exists.

For proof the agent extends `tests/e2e/auth.test.ts`, whose four tests check the headings, labels, buttons and links on `/login` and `/register`, with an assertion that the GitHub button is visible on both pages; the OAuth round trip itself needs GitHub and cannot run in CI. In the sandbox it installs Postgres, sets `POSTGRES_URL` and `AUTH_SECRET`, runs `pnpm install`, `pnpm db:migrate`, `pnpm check` for Biome through ultracite, `pnpm build`, and then `pnpm dev` to open `/login` in its browser, click the button and watch the redirect land on github.com's authorize page carrying the configured client id, which proves the provider is wired without a real OAuth app. It runs the Playwright suite with `pnpm test` and quotes the result.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Add sign in with GitHub using Auth.js. Read app/(auth)/auth.ts, app/(auth)/auth.config.ts, app/(auth)/actions.ts, app/(auth)/login/page.tsx, app/(auth)/register/page.tsx, components/chat/auth-form.tsx, proxy.ts, lib/db/schema.ts, lib/db/queries.ts, lib/constants.ts, .env.example and tests/e2e/auth.test.ts first. Add the GitHub provider configured from AUTH_GITHUB_ID and AUTH_GITHUB_SECRET and document both in .env.example. Keep the JWT strategy: in a signIn callback, find the user by email or create one with no password and emailVerified set; in the jwt callback, set token.id to the database row's id and token.type to regular for GitHub sign-ins. Add a Continue with GitHub button to the login and register pages through a server action. Extend the auth e2e tests to expect the button. Run pnpm check, pnpm build and pnpm test against a local Postgres, open /login in a browser and confirm the redirect to GitHub's authorize page, quote the results and open a pull request.",
  "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 `app/(auth)/auth.ts`, `app/(auth)/actions.ts`, the two auth pages, `lib/db/queries.ts`, `.env.example` and `tests/e2e/auth.test.ts`. No schema migration: the columns the GitHub profile needs are already in the `User` table, and the session stays a JWT with no adapter tables.

The description quotes the Biome check, the `next build` output, the Playwright summary with the extended auth tests, and the URL the browser reached after clicking the button, showing GitHub's authorize endpoint with the client id from the environment. The repository's `Lint` and `Playwright Tests` workflows run on the branch and appear beside the agent's record.

Before merging, a reviewer registers the OAuth app on GitHub with the callback `/api/auth/callback/github`, decides whether an existing password account with the same email should be linked on first GitHub sign-in, which the agent does by email match and flags, and whether a guest's chats should move to the account they sign in to, which the agent leaves as a separate change.

## Run it on every Linear issue labelled auth

In the Project that holds the repository, create an automation with a general version of this page's prompt and a `linear` trigger on `linear.issue.labeled` with `auth` in its `labels`. The trigger puts the issue after the saved prompt and fills nothing in, so the prompt says: read the issue that follows, add or change the Auth.js provider it describes without changing the session strategy, extend the auth tests, prove the redirect in a browser, and open a pull request that links the issue. It needs the runner's Linear connection, made in the console under Settings → Connections, and fires only when someone in your Rush org adds the label.

An issue for "sign in with Google" starts a second run on its own branch using the same callbacks; an issue about linking an existing email account starts a third. Each pull request links the issue it came from.

## Why run it on Rush?

Checking an OAuth provider means a running app with Postgres behind it and a browser to click the button and read the address it lands on, plus the Biome check and the build that the repository's workflows run. The sandbox has all of it, and the agent's own browser session is kept, so the pull request opens with the redirect URL and the test output in it.

The run's record holds every command and the browser session, so a reviewer sees the authorize redirect rather than trusting a sentence. Across a team's Next.js apps the same request runs at once, one sandbox each, and each pull request names that app's own tables and callbacks.

## Frequently asked questions

### Why not add the Drizzle adapter and the account table?

The app uses the JWT session strategy and its own `User` table with chat rows keyed on it; an adapter would add four tables and change how ids flow. Matching by email in `signIn` and fixing `token.id` in `jwt` keeps the existing queries and the guest flow untouched.

### Can a GitHub user also sign in with a password?

Not until they set one. The row is created with `password` null, and `authorize` returns null for a user without a password after a dummy compare, so the credentials form rejects them without revealing the account exists. A set-password flow is a separate change.

### What happens to a guest who signs in with GitHub?

They get a regular account and the proxy stops sending them to `/api/auth/guest`. Their guest chats stay on the guest row; moving them is a decision the pull request flags rather than makes.

### Does the e2e suite test the GitHub round trip?

No. It asserts the button renders on both pages. The round trip needs GitHub's consent screen, so the agent proves the wiring by following the redirect in its own browser and quoting the authorize URL.

## More NextAuth.js jobs

- [Add Google Login to a Next.js App with NextAuth.js](https://getrush.ai/run/add-google-login/next-auth/)
- [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/)

## More jobs like this: Turn a Linear label into a PR

- [Add a Global Keyboard Shortcut to a Tauri App](https://getrush.ai/run/add-global-shortcut/tauri/)
- [Add golangci-lint to a Go CLI's CI](https://getrush.ai/run/add-golangci-lint-to-ci/go-cli/)
- [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/)
