# Add User Registration to a Fastify App

Yes. An agent adds POST /api/auth/register with a TypeBox schema, hashes with the app's scrypt helper, assigns the basic role, tests it and opens a PR.

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

## What the agent does

The agent starts from how the app already logs people in. In a service shaped like fastify/demo, `package.json` runs Fastify 5 on TypeScript with `@fastify/autoload`, `@fastify/type-provider-typebox`, `@fastify/session` and `knex` over `mysql2`, and its `test` script seeds the database then runs `tsx --test` under `c8`. `src/routes/api/auth/index.ts` has one route, `POST /login`, which looks a user up with `usersRepository.findByEmail`, compares the password with `passwordManager.compare` and writes `id`, `email`, `username` and `roles` into the session. There is no way to create a user over HTTP: `scripts/seed-database.ts` inserts `basic`, `moderator` and `admin` directly, and `src/routes/api/autohooks.ts` rejects every `/api` request without a session except `/api/auth/login`.

The agent adds `POST /register` next to `/login`. The body schema goes in `src/schemas/auth.ts` as `RegisterSchema`, built from the existing `EmailSchema`, a `username` string and the `PasswordSchema` pattern already defined in `src/schemas/users.ts` (one upper case letter, one lower case letter, one digit, one symbol, at least 8 characters), so the password rule for new accounts matches the rule for changing one. The handler runs inside `fastify.knex.transaction` like login does: it calls a new `usersRepository.findByEmail` check and answers `409` when the email is taken, hashes the password with `passwordManager.hash` (the scrypt helper in `src/plugins/app/password-manager.ts`), inserts the row, then inserts a `user_roles` row linking the new user to the `basic` role from `migrations/004.do.roles.sql` and `005.do.user_roles.sql`. It answers `201` with `{ id }`, and it does not sign the user in, so the client follows with `/login` as before.

Two things need an exception. `autohooks.ts` lets `/api/auth/login` through by prefix; the agent extends that check to `/api/auth/register` so the route is reachable without a session. And the route gets its own `config.rateLimit` of a few attempts per minute, the same shape `PUT /api/users/update-password` already uses, because `@fastify/rate-limit` is registered globally with `RATE_LIMIT_MAX` from `.env` and a registration endpoint is the one a bot hammers. The repository gains `create` and `findRoleByName` methods beside `findByEmail`, `updatePassword` and `findUserRolesByEmail`, so no SQL lands in the route file.

Tests follow `test/routes/api/auth/auth.test.ts` and `test/routes/api/users/users.test.ts`: `build(t)` from `test/helper.ts`, `app.inject`, and a cleanup that deletes the row by username in `afterEach`. The new cases register a user and read back the `201` id, register the same email twice and expect `409`, send a weak password and use `expectValidationError`, log in with the new credentials to prove the hash round-trips through `compare`, and hit the route four times to see the `429`. The agent runs `npm run lint`, `npm run build -- --noEmit` and `npm test` against a MySQL started from `docker-compose.yml`, the same commands `.github/workflows/ci.yml` runs on Node 22 and 24, and puts their output in the pull request.

## Start it with one API call

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

```json
{
  "agent": "codex",
  "input": "Add user registration to this Fastify app and open a pull request. Read package.json, src/app.ts, src/routes/api/auth/index.ts, src/routes/api/autohooks.ts, src/routes/api/users/index.ts, src/schemas/auth.ts, src/schemas/users.ts, src/plugins/app/users/users-repository.ts, src/plugins/app/password-manager.ts, migrations/001.do.users.sql, migrations/004.do.roles.sql, migrations/005.do.user_roles.sql, scripts/seed-database.ts and test/helper.ts first. Add POST /api/auth/register with a TypeBox body of email, username and password, reusing the existing password pattern. Inside a knex transaction, answer 409 when the email exists, hash the password with passwordManager.hash, insert the user, link it to the basic role, and answer 201 with the new id. Do not create a session on register. Let the route through the autohooks session check and give it a per-route rateLimit like update-password has. Put the SQL in users-repository.ts as create and findRoleByName. Add tests in test/routes/api/auth/ using build and inject: success, duplicate email, weak password, login with the new account, and the 429. Run npm run lint, npm run build -- --noEmit and npm test, and paste the output in the pull request. Change nothing else.",
  "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 touching `src/routes/api/auth/index.ts`, `src/routes/api/autohooks.ts`, `src/schemas/auth.ts`, `src/plugins/app/users/users-repository.ts` and a new or extended test file under `test/routes/api/auth/`. The description shows the `node:test` summary from `npm test` with the c8 coverage table, the eslint result from `neostandard`, and an example `curl` of the new route with its `201` body.

On a repository with the demo's workflow, the pull request runs the matrix on Node 22 and Node 24 against the `mysql:8.4` service, with `RATE_LIMIT_MAX` set to 4 as the workflow does, so the rate-limit test is exercised under the same limit the suite expects. The existing login and update-password tests run unchanged because the seed script and the three seeded users are untouched.

A reviewer checks that the duplicate-email answer does not leak whether an address is registered in a way they object to, that the insert and the role link are in one transaction, that the response omits the hash, and that the route sits outside the session check but inside the rate limit. Then they decide whether to require email verification before the account can log in, which this change leaves as a follow-up.

## Run it on every new API service

A team that stands up Fastify services from the same template sends one `POST /o/{org}/sessions` per repository with this prompt and that repository in `resources`. Each run reads its own `users-repository.ts` and migrations, so a service with a different role table gets the link written against its own schema, and each run opens its own pull request.

Give the API key a `webhook_url` and Rush POSTs `run.completed` when each turn finishes, with the `session_url` to read the pull request link and the test output from.

## Why run it on Rush?

A registration route is small but it touches the three things a team argues about: the password rule, the transaction boundary and what the route answers when the email is taken. In a Rush sandbox the agent runs the real suite against a real MySQL, including the `429` case that a developer tends to skip locally because it needs the limit lowered, and the full record of every command and its output is kept with the run.

The same request works unchanged on every service a team owns, and because the run clones the repository through the Rush GitHub App and opens a pull request, nobody's laptop needs the database, the `.env` or the seed step configured to get a reviewable change.

## Frequently asked questions

### Will registering sign the user in?

No. The route answers `201` with the new id and leaves the session empty, so the client calls `/api/auth/login` as it does today. Signing in on register is a one-line change the pull request points out if a team wants it.

### Which role does a new user get?

The `basic` role from the roles table, looked up by name inside the same transaction as the insert. The seed script's hierarchy, where admin also holds moderator and basic, is untouched.

### Does it change the session check in autohooks.ts?

Only to add `/api/auth/register` to the paths allowed without a session. Every other `/api` route still answers `401` through `reply.unauthorized` when there is no logged-in user.

### Does it add a dependency?

No. The schema uses TypeBox, which is already installed, hashing uses the existing scrypt helper, and the rate limit uses the `@fastify/rate-limit` plugin the app registers globally.

## More Fastify jobs

- [Add WebSockets to a Fastify App](https://getrush.ai/run/add-websockets/fastify/)
- [Dockerize a Fastify App With MySQL](https://getrush.ai/run/dockerize-app/fastify/)
- [Fix a Failing CI Build in a Fastify API](https://getrush.ai/run/fix-failing-ci/fastify/)

## The same job on other stacks

- [Add User Registration to a Symfony App from a Linear Issue](https://getrush.ai/run/add-user-registration/symfony/)
- [Add User Registration to a Vapor App](https://getrush.ai/run/add-user-registration/vapor/)
- [Add User Registration to a CodeIgniter App](https://getrush.ai/run/add-user-registration/codeigniter/)

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