# Add User Registration to a Vapor App

Yes. An agent adds a User model, migration and register route to your Vapor 4 app, hashes passwords with Bcrypt, covers it with VaporTesting and opens a PR.

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

## What the agent does

The agent reads the project before it adds a model. In an app generated from vapor/template with Fluent, `Package.swift` (swift-tools-version 6.3, macOS 13) depends on `vapor` from 4.121.4, `fluent` from 4.13.0 and one `fluent-<db>-driver`: Postgres 2.12.0, MySQL 4.8.0 or SQLite 4.9.0, the three choices `manifest.yml` offers. The test target depends on `VaporTesting`, and the Swift settings turn on `ExistentialAny`, `InternalImportsByDefault` and `MemberImportVisibility`, so every new file needs explicit `import Fluent` and `import Vapor` lines and `any` on protocol types. `Sources/App/configure.swift` reads `DATABASE_HOST`, `DATABASE_PORT`, `DATABASE_USERNAME`, `DATABASE_PASSWORD` and `DATABASE_NAME` from the environment and registers `CreateTodo`; `routes.swift` mounts `GET /`, `GET /hello` and `TodoController`. The only model is `Todo` with a `title` field, mapped through `TodoDTO`. There is no user, no password hashing and no authentication.

It then adds the smallest set of files that follows the Todo layout. `Sources/App/Models/User.swift` is a `final class User: Model, @unchecked Sendable` on the `users` schema with `@ID`, `@Field(key: "email")`, `@Field(key: "password_hash")` and `@Timestamp(key: "created_at", on: .create)`, plus a `toDTO()` that returns id and email and never the hash. `Migrations/CreateUser.swift` is an `AsyncMigration` whose `prepare` calls `.id()`, two `.field(...)` lines and `.unique(on: "email")`, and whose `revert` drops the table; `configure.swift` registers it after `CreateTodo`. `DTOs/RegisterUserDTO.swift` is a `Content` that also conforms to `Validatable`, with `validations.add("email", as: String.self, is: .email)` and a password rule of `.count(8...)`.

`Controllers/UserController.swift` is a `RouteCollection`, like `TodoController`, with one route: `users.post("register")`. The handler calls `try RegisterUserDTO.validate(content: req)`, hashes with `req.password.async.hash(dto.password)` so Bcrypt work leaves the event loop, saves the `User`, and answers `201 Created` with the `UserDTO`. A second registration with the same email hits the unique index; the handler catches that database error and throws `Abort(.conflict)` rather than letting a 500 through. `routes.swift` gains `try app.register(collection: UserController())`. Nothing else in `configure.swift` changes: Vapor's default hasher is already Bcrypt at cost 12, so no `app.passwords.use` line is needed.

Tests go where `AppTests.swift` already is. That file uses Swift Testing, `@Suite("App Tests with DB", .serialized)` and a `withApp` helper that runs `configure`, `autoMigrate`, the test body and `autoRevert`. The agent adds cases in the same style: a valid body answers 201 with an email and no `passwordHash` key, the stored hash verifies with `app.password.verify(_:created:)` and is not the plaintext, the same email twice answers 409, and a malformed email or a seven-character password answers 400 with Vapor's validation message. It runs `swift build` and `swift test` in the sandbox with the toolchain the `Dockerfile` names, `swift:6.3-noble`, against the database the project is configured for, and pastes both outputs in the pull request.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Add user registration to this Vapor 4 app and open a pull request. Read Package.swift, Sources/App/configure.swift, routes.swift, the Todo model, DTO, migration and controller, and Tests/AppTests first, and follow their layout exactly. Add a User model (users table: id, email, password_hash, created_at), a CreateUser migration with a unique index on email registered after CreateTodo, a RegisterUserDTO that is Validatable (email must be an email, password at least 8 characters), a UserDTO that never includes the hash, and a UserController with POST /users/register that validates, hashes with req.password.async.hash, saves, and answers 201 with the UserDTO; a duplicate email answers 409. Register the controller in routes.swift. Add Swift Testing cases beside the existing ones: 201 with no hash in the body, the stored hash verifies and is not the plaintext, duplicate email is 409, invalid email and short password are 400. Run swift build and swift test and paste the output in the pull request. Do not add login, sessions or tokens, and do not change the Todo code or the Dockerfile.",
  "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 with four new files under `Sources/App` (`Models/User.swift`, `Migrations/CreateUser.swift`, `DTOs/RegisterUserDTO.swift`, `Controllers/UserController.swift`), two one-line changes in `configure.swift` and `routes.swift`, and new `@Test` cases in `Tests/AppTests/AppTests.swift`. The description quotes the `swift test` summary from the sandbox and shows one `curl` of `POST /users/register` with its 201 body.

A project generated from the template has no workflow of its own: `test-template.yml` lives in the template repository and tests `vapor new` itself, so the pasted output is the check unless the team has added a `swift test` workflow, in which case it runs on the pull request too.

A reviewer checks that `UserDTO` has no hash field, that the migration carries the unique index rather than relying on a lookup before insert, that the 409 comes from the database error and not from a race-prone pre-check, and that the validation rules match the product's password policy. They also confirm no login route was added, since registration without a session is the stated scope.

## Run it on every new service from the template

A team that starts services with `vapor new` sends one `POST /o/{org}/sessions` per new repository with this prompt and that repository in `resources`. Each run reads its own `Package.swift` and database driver, so a SQLite service and a Postgres service each get a migration that works on their driver, and each opens its own pull request. Send a `request_id` with each call so a retried request returns the run already started instead of a second one.

Give the API key a `webhook_url` and Rush POSTs `run.completed` when each turn ends, with the `session_url` to read the pull request from. A run that could not finish, for example because the project was generated with `--no-fluent` and has no model layer, ends `needs_review` and says so in its result.

## Why run it on Rush?

Registration is where a password first touches your code, and the mistakes are quiet: a hash in a response body, a uniqueness check that races, a 500 where a 409 belongs. In a Rush sandbox the agent compiles with the same Swift 6.3 toolchain the `Dockerfile` uses, runs `swift test` against a real database, and the commands and their output stay in the run's record beside the diff, so the reviewer reads the test run rather than trusting the description.

Each piece is small, but a Swift build with Fluent and a database takes real time on a laptop, and the new files must follow the project's own layout and the upcoming-feature flags in `Package.swift`. The agent does the whole pass in one go, and the same request works unchanged on the next Vapor service the team generates.

## Frequently asked questions

### Does it add login as well?

No. The scope is registration: model, migration, validation, hashing and one route. Login with `ModelAuthenticatable` and a session or token is a follow-up, which you can send to the same run with `POST /o/{org}/sessions/{id}/messages` once this turn has finished.

### How are passwords hashed?

With Vapor's default Bcrypt hasher at cost 12, through `req.password.async.hash`, which runs on a background thread pool rather than the event loop. The hash is stored in `password_hash` and verified later with `verify(_:created:)`; it never appears in a DTO.

### Does it work with SQLite as well as Postgres and MySQL?

Yes. The migration uses Fluent's schema builder, so `.unique(on: "email")` and the fields are the same code on all three drivers the template offers. The tests run against whichever driver `Package.swift` names, with the connection settings `configure.swift` already reads from the environment.

### What if my project was generated with --no-fluent?

Then there is no database layer to put a user in. The agent stops at `needs_review` and says so rather than adding Fluent and a driver on its own, because choosing a database is your decision.

## More Vapor jobs

- [Add a WebSocket Endpoint to a Vapor App from a Linear Issue](https://getrush.ai/run/add-websockets/vapor/)
- [Write Tests for a Vapor Server](https://getrush.ai/run/write-vapor-tests/vapor/)
- [Add Consistent API Error Responses to a Vapor App](https://getrush.ai/run/add-api-error-handling/vapor/)

## The same job on other stacks

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

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