# Add Google Login to an Express App with Passport

Yes. An agent adds a Google strategy beside the password login, stores the link in its own table, tests every step it can without Google and opens a PR.

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

## What the agent does

The agent reads how sign-in works today before adding a second way in. In an app like passport/todos-express-password, `routes/auth.js` configures `passport-local`, serialises the user into the session as `{ id, username }`, and serves `/login`, `/login/password`, `/logout` and `/signup`. `app.js` keeps sessions in SQLite through `connect-sqlite3`, puts `csurf` in front of every route and restores the user with `passport.authenticate('session')`. `db.js` creates a `users` table with `username`, `hashed_password` and `salt`, using `CREATE TABLE IF NOT EXISTS`, and `views/index.ejs` prints `user.username` in the navigation bar. Every one of those assumes a user has a username and a password, and a person who arrives from Google has neither.

It follows the pattern the Passport project uses in its own Google example. The strategy is `passport-google-oidc`, configured with `GOOGLE_CLIENT_ID` and `GOOGLE_CLIENT_SECRET` from the environment, the relative callback `/oauth2/redirect/google` and the `profile` scope. Its verify function receives the issuer and the profile and looks the pair up in a new `federated_credentials` table with a unique constraint on provider and subject. An unknown pair creates a user and the link; a known one loads the user. The agent adds a `name` column for the display name, and because `CREATE TABLE IF NOT EXISTS` does nothing to a `users` table that already exists in `var/db/todos.db`, it adds the column with a guarded `ALTER TABLE` so existing installs do not break on first sign-in. `serializeUser` gains `name`, and the navigation bar shows the name when there is no username.

Then come the parts that make it safe to merge. With no client id in the environment the strategy's constructor throws and the app does not start, so the agent registers the strategy and renders the button in `views/login.ejs` only when both variables are set; a developer without Google credentials still gets the password login. It adds a guard to the password strategy as well. That verify function passes `row.salt` to `crypto.pbkdf2`, which throws on a null salt instead of failing the login, so a row with no password is rejected before the hash is attempted. The Google link is a plain GET, and so is the callback, so `csurf` needs no exemption, and the `state` value the strategy keeps in the session protects the callback.

Testing is honest about its limit: the sandbox has no Google client and no Google account, so the consent screen is out of reach. Everything around it is not. With placeholder values in the environment, the agent requests `/login/federated/google` and checks the 302 to `accounts.google.com` carries `response_type=code`, the client id, a `redirect_uri` that ends in `/oauth2/redirect/google`, the `openid profile` scope and a `state`. It calls the callback with a forged `state` and expects a redirect back to `/login`. It runs the verify function against a scratch database: a new subject creates one user and one credential, and the same subject again returns that user and creates nothing. It starts the app with the variables unset to confirm it boots without the button, and signs in as the seeded user to confirm the password path and its CSRF check are unchanged.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Add Sign in with Google to this Express and Passport app, beside the existing username and password login, and open a pull request. Read package.json, app.js, db.js, routes/auth.js, routes/index.js, bin/www, .gitignore and every file under views first. Follow the approach of the Passport project's own Google example: the passport-google-oidc strategy, GOOGLE_CLIENT_ID and GOOGLE_CLIENT_SECRET from the environment, routes GET /login/federated/google and GET /oauth2/redirect/google, and a federated_credentials table that links a provider and subject to a user. Decide how a user with no username or password is stored and displayed, and make the schema change work on a database file that already exists, since db.js only creates tables that are missing. Make the app start and the password login work when the Google variables are not set, and show the Google button only when they are. Check what the password strategy does with a user row that has no salt and make it fail the login cleanly. Say how CSRF protection and the OAuth state parameter apply to the two new routes. You have no Google credentials: use placeholder values and test what can be tested without Google's consent screen, namely the redirect and each of its query parameters, a callback with a forged state, the verify function for a new and a returning subject against a scratch database, startup with and without the variables, and the unchanged password login. Do not link Google accounts to existing password accounts automatically; explain why and what linking would need. Document the redirect URI to register and the variables to set in the README. List in the pull request exactly what a reviewer must test with real credentials.",
  "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

The pull request changes `package.json`, `routes/auth.js`, `db.js`, `views/login.ejs`, `views/index.ejs` and the README. Its description walks the sign-in flow in order and gives the evidence for each step the agent could reach: the redirect's `Location` header with its query parameters, the response to a forged `state`, the rows in `users` and `federated_credentials` after the verify function ran twice for one subject, and the app's startup output with and without the Google variables.

It then lists what it did not prove, as a checklist for the reviewer. Create an OAuth client in Google Cloud, register `http://localhost:3000/oauth2/redirect/google` as an authorised redirect URI (3000 is the default port in `bin/www`), put the two values in `.env`, which `.gitignore` already excludes, and sign in. Check that a first sign-in lands on the todo list with the Google display name in the navigation bar, that a second sign-in reuses the same user, and that signing out and back in with the seeded password account still works.

Two design choices are flagged for a decision. The strategy asks only for the `profile` scope, so the app never sees an email address and cannot match a Google sign-in to an existing password account; the same person using both gets two accounts until linking is built. And the callback URL is relative, so the absolute address sent to Google is built from the incoming request. Behind a proxy that terminates TLS, the reviewer confirms it comes out as `https` and matches what is registered, or sets an absolute URL.

## Run it on every app that needs a sign-in provider

This is started on purpose, per repository, so the API is the natural way in. Send `POST /o/{org}/sessions` with this prompt and the repository in `resources`. An agency or platform team that maintains several Express apps on Passport sends one request per repository; each run reads that app's own user table and views, so one with an email column or a different session store gets a change that fits it. Give the key a `webhook_url` and each run it starts posts `run.completed` when its turn ends.

The reviewer's test with real credentials is the natural point for a second turn. If the real sign-in shows something the placeholders could not, such as a redirect URI mismatch behind your proxy, send the error text as a follow-up with `POST …/sessions/{id}/messages`. That wakes the parked sandbox with the branch, the dependencies and the scratch database still in place, and the fix lands on the same pull request. Parked sandboxes are kept for 7 idle days.

## Why run it on Rush?

Sign-in code fails at the edges: the app that will not boot without a variable, the old database file with no new column, the password route that throws on a row it never expected. Each of those is found by running the app in that state, not by reading the diff. In a Rush sandbox the agent starts the server several ways, sends real requests with a cookie jar and a CSRF token, and inspects the SQLite files afterwards, and every command and response is kept in the run's record.

It also draws the line between tested and untested where a person can see it. An agent on Rush cannot sign in to Google as you, and the pull request does not pretend otherwise. It says which steps ran, shows their output, and hands the reviewer a short list for the one step that needs real credentials, which is a better starting point than a branch that claims to work.

## Frequently asked questions

### Which Passport strategy does it use for Google?

`passport-google-oidc`, the one the Passport project's own Google example app uses. It signs users in with OpenID Connect and gives the verify function an issuer and a profile. If your codebase already uses `passport-google-oauth20`, say so in the prompt and the agent follows that strategy's verify signature instead.

### Can the agent test with my real Google credentials?

It can read them, but it still cannot finish a sign-in. Save the two values as secrets on a Project and start the session there with `POST /o/{org}/p/{project}/sessions`, and the agent sees them as environment variables. Completing the flow also needs a Google account to consent, which the agent does not have, so the round trip stays with the reviewer.

### Will existing users be able to link their Google account?

Not from this change. It creates a new user for a Google identity it has not seen. Linking means letting a signed-in user start the Google flow and attaching the result to their current account, plus deciding what to do when that Google identity already has one. The pull request describes the work and leaves it for a follow-up.

### Does adding Google login weaken the CSRF protection?

No. `csurf` keeps checking every form post, including the password login. The two new routes are GET requests, and the callback is protected by the `state` value the strategy stores in the session and compares when Google redirects back. The agent tests that a callback with the wrong `state` is refused.

## More Passport.js jobs

- [Fix "Error: req#logout requires a callback function" in Passport 0.6 and later](https://getrush.ai/run/fix-req-logout-requires-a-callback-function/passport/)
- [Review Pull Requests That Change Passport.js Login Code](https://getrush.ai/run/review-pull-requests/passport/)
- [Add Account Lockout to a Passport Local Login](https://getrush.ai/run/add-account-lockout/passport/)

## The same job on other stacks

- [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/)
- [Add Google Login to an Express and Mongoose API](https://getrush.ai/run/add-google-login/mongoose/)

## 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/)
