# Add Cookie Sessions to a React Router App

Yes. An agent adds cookie session storage, a login action, a logout route and a loader guard to your React Router framework-mode app, tests the flow, and opens a PR.

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

## What the agent does

The agent reads the template before it adds a session to it. In an app shaped like the `default` template of remix-run/react-router-templates, `package.json` depends on `react-router`, `@react-router/node` and `@react-router/serve` at `*`, `react` and `react-dom` 19.2.8 and `isbot`, with `@react-router/dev`, `vite` 8.0.3, `tailwindcss` 4.2.2 and `typescript` 5.9.3 as dev dependencies, and four scripts: `build`, `dev`, `start` with `react-router-serve ./build/server/index.js`, and `typecheck`, which runs `react-router typegen && tsc`. `react-router.config.ts` sets `ssr: true`. `app/routes.ts` has one entry, `index("routes/home.tsx")`, and `app/root.tsx` renders `Layout`, `App` with an `Outlet`, and an `ErrorBoundary` that shows a 404 message for route errors. There is no server state anywhere: no session, no cookie, no user.

Two things in the repository shape how the change is checked. `.gitignore` already ignores `.env`, so a secret can live there without a new rule. `.tests/test.default.ts` is the repository's own Playwright harness: it copies the template into a temporary directory, runs `pnpm typecheck`, starts `pnpm dev` and `pnpm build` plus `pnpm start` on a free port, and checks the page title and two links while asserting that `stderr` stays empty. `.github/workflows/ci.yml` runs that harness with pnpm 10 on Node 22 and Chromium, so a session that logs a warning on start would fail the build.

The agent adds `app/sessions.server.ts` with `createCookieSessionStorage` from `react-router`, typed with a `SessionData` of `userId` and a flash `error`, cookie name `__session`, `httpOnly`, `sameSite: "lax"`, `path: "/"`, `secure` when `NODE_ENV` is `production`, and `secrets` from `SESSION_SECRET`, throwing at startup in production when it is unset and falling back to a development-only value otherwise. `app/auth.server.ts` holds `requireUserId(request)`, which reads the session and throws a `redirect` to `/login?redirectTo=` with the current path when there is no user, and a `verifyLogin` the reader replaces with their user store. Three routes join `app/routes.ts`: `routes/login.tsx` with a form, an `action` that sets `userId` or a flash error and commits the cookie in `Set-Cookie`, `routes/dashboard.tsx` whose `loader` calls `requireUserId`, and `routes/logout.tsx` whose `action` destroys the session. `meta` exports give each page a title the way `routes/home.tsx` already does, and `react-router typegen` produces the `+types` the components import.

The agent extends the harness rather than adding a second one: `.tests/test.default.ts` gains a `workflow` step that visits `/dashboard`, expects the URL to end in `/login?redirectTo=%2Fdashboard`, submits the form, expects the dashboard heading, posts the logout form and expects `/login` again, in both the `dev` and the `build + start` runs with `SESSION_SECRET` in `env`. It adds `.env.example` with that one variable and a paragraph to `README.md`, runs `pnpm typecheck`, `pnpm build` and `pnpm test --project=chromium` from `.tests`, and keeps the output.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Add cookie-based sessions with login and logout to this React Router framework-mode app and open a pull request. Read package.json, react-router.config.ts, app/routes.ts, app/root.tsx, app/routes/home.tsx, .gitignore and .tests/test.default.ts first. Create app/sessions.server.ts with createCookieSessionStorage from react-router, cookie __session, httpOnly, sameSite lax, secure in production, secrets from SESSION_SECRET with a startup error in production when it is missing. Create app/auth.server.ts with requireUserId that redirects to /login with redirectTo, and a verifyLogin stub with a clear comment for replacing it. Add routes/login.tsx with a form and an action that sets the session or a flash error, routes/dashboard.tsx whose loader requires a user, and routes/logout.tsx whose action destroys the session, and register them in app/routes.ts with meta titles. Add .env.example and a README section. Extend .tests/test.default.ts so the dev and build runs visit /dashboard, get redirected, log in, see the dashboard and log out, with SESSION_SECRET set. Run pnpm typecheck, pnpm build and pnpm test --project=chromium in .tests, and paste the output in the 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 adds two `.server.ts` modules and three route files under `app/`, three lines in `app/routes.ts`, a `.env.example`, a README section and a longer `workflow` in `.tests/test.default.ts`. The description shows the cookie options as they are set, explains where `verifyLogin` is meant to be replaced, and pastes the `typecheck`, `build` and Playwright output.

The repository's `CI` workflow runs the harness on the pull request, so the new login steps run in Chromium on Node 22 as a check, with the template copied fresh the way the harness already does it.

A reviewer checks that the cookie is `httpOnly` and `secure` in production, that `requireUserId` returns to the page the user asked for after login, that a wrong password shows the flash message without a stack trace, and that `SESSION_SECRET` is read once at module load. They merge when the harness is green and replace `verifyLogin` in a follow-up against their own user table.

## Run it on every React Router app that needs a signed-in user

A team starting several React Router apps sends one `POST /o/{org}/sessions` per repository with this prompt and the repository in `resources`. Each run reads that app's `routes.ts` and adds the three routes beside what is there, so an app that already has a `login` route gets the guard and logout only, and each run opens its own pull request.

Add a `request_id` to each call so a retried request returns the run it already started, and set a `webhook_url` on the key so your server receives `run.completed` with `session_url` when each turn ends and can post the pull request where the team reads it.

## Why run it on Rush?

A session is small code with a large surface for mistakes: a cookie that is not `httpOnly`, a secret read from the wrong place, a redirect that drops the page the user wanted. The only convincing check is a browser walking the flow against both the dev server and the built server, which is what the repository's own harness does. In a Rush sandbox the agent runs that harness in Chromium and keeps its output in the run's record beside the pull request.

By hand this is an hour of reading the React Router session docs and wiring five files together while `typegen` and `tsc` complain. The agent makes the change in one pass, and when the reader wants `verifyLogin` pointed at their database, a follow-up wakes the same parked sandbox and lands in the same pull request.

## Frequently asked questions

### Why cookie sessions rather than a database session table?

The template has no database, and `createCookieSessionStorage` keeps the whole session, here one `userId`, signed inside the cookie with `SESSION_SECRET`. When the app grows a database, `createSessionStorage` with a custom store or a database-backed storage can replace the module without changing the routes, because they only call `getSession`, `commitSession` and `destroySession`.

### Where does the password get checked?

In `verifyLogin` in `app/auth.server.ts`, which the pull request ships as a stub with a comment saying so. The template has no users, so the agent does not invent a table. The login action, the redirect and the cookie are complete; the reader wires the one function to their store.

### Does the node-custom-server template need something else?

No. The session modules only use `Request` and `Response`, so they work under `@react-router/serve` and under the Express server in `server/app.ts` alike. If the reader's repository is the custom-server template, the agent extends `.tests/test.node-custom-server.ts` instead.

### What if SESSION_SECRET is missing?

In production the module throws when it loads, so `pnpm start` fails at once with a message naming the variable rather than signing cookies with a default. In development it falls back to a fixed string so `pnpm dev` works out of the box, and `.env.example` shows the name to set.

## More React Router jobs

- [Add a Dark Mode Toggle to a React Router App](https://getrush.ai/run/add-dark-mode/react-router/)
- [Add Error Boundaries to a React Router App](https://getrush.ai/run/add-error-boundaries/react-router/)
- [Add File Uploads to a React Router App](https://getrush.ai/run/add-file-uploads/react-router/)

## The same job on other stacks

- [Add Cookie Sessions to an aiohttp App](https://getrush.ai/run/add-cookie-sessions/aiohttp/)

## More jobs like this: Hand off a long task

- [Add a Copy Button to Sphinx Code Blocks](https://getrush.ai/run/add-copy-button/sphinx/)
- [Add CORS to an Actix Web API](https://getrush.ai/run/add-cors/actix/)

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