# Add JWT Authentication to a Fiber API

Yes. An agent adds a login route that issues HS256 tokens and the gofiber/contrib JWT middleware on your Fiber v3 routes, with handler tests, and opens a PR.

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

## What the agent does

The agent reads the app before choosing a library, because Fiber v3 has its own middleware module and importing a v2 one compiles against the wrong `fiber.Ctx`. In an app like gofiber/boilerplate, `go.mod` pins `gofiber/fiber/v3` at v3.6.1 under `go 1.26.0`, and `app.go` builds the app with `recover` and `logger` middleware, a `/api/v1` group with `GET /users` and `POST /users` from `handlers/handlers.go`, a static handler for `./static/public` and a custom 404. `database/database.go` is an in-memory slice behind a mutex, `models/user.go` is a `User` with one `Name` field, and there is no password, session or token anywhere. `.github/workflows/test.yml` runs `go test ./... -v -race` on Go 1.26 and 1.27, and `lint.yml` runs golangci-lint v2.14.0 with `--tests=false`.

The agent adds the `github.com/gofiber/contrib/v3/jwt` module, the JWT middleware written for Fiber v3, and `github.com/golang-jwt/jwt/v5`, which it uses to sign. It writes `handlers/auth.go` with `POST /api/v1/auth/login`: read `user` and `pass` from the form the way `UserCreate` already reads `user` with `c.FormValue`, check them against a credential source, and answer `{"token": ...}` with an HS256 token whose claims hold the user name and an `exp` of a configurable number of hours. Because the boilerplate stores no credentials, the agent reads the password from a `JWT_LOGIN_PASSWORD` environment variable beside a `JWT_SECRET` one, refuses to start when either is empty, and says in the pull request that a real app replaces that check with a lookup in its own user store and a `bcrypt` comparison.

Protection goes on the routes that change state. `app.go` keeps `GET /users` open and wraps `POST /users` with `jwtware.New(jwtware.Config{SigningKey: jwtware.SigningKey{Key: secret, JWTAlg: "HS256"}})`, registered on the `/api/v1` group after the `auth` routes so a login never needs a token. The middleware reads `Authorization: Bearer` by default, answers `400` with a `WWW-Authenticate` challenge when the header is missing and `401` when the token is bad or expired, and `jwtware.FromContext(c)` gives the handler the parsed `*jwt.Token` so `UserCreate` can record who created the user. The agent sets `JWTAlg` so the middleware rejects any other `alg` before it looks up a key, and leaves the static routes and the 404 handler untouched.

The tests go in `handlers/auth_test.go` and `handlers/handlers_test.go` using `app.Test` with `httptest.NewRequest`, the way Fiber's own examples do: a login with the right password returns a token the middleware accepts on `POST /users`; a wrong password returns `401`; a missing header on `POST /users` returns `400`; an expired token returns `401`; and `GET /users` works with no token at all. The agent runs `go vet ./...`, `go test ./... -v -race` as `test.yml` does, `golangci-lint run --tests=false` at the version `lint.yml` names, and `gosec ./...` as `security.yml` does, then starts the app with `go run app.go` and shows the `curl` sequence in the pull request.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Add JWT authentication to this Fiber v3 API. Read go.mod, app.go, handlers/handlers.go, database/database.go, models/user.go, Makefile, Dockerfile, .golangci.yml and every workflow under .github/workflows first. Use the Fiber v3 JWT middleware from gofiber/contrib and golang-jwt/jwt v5. Add POST /api/v1/auth/login that checks credentials from environment variables, since this app has no user store with passwords, and returns an HS256 token with an expiry; refuse to start when the secret is empty. Protect POST /users with the middleware, pin the algorithm in the config, leave GET /users, the static files and the 404 handler open, and read the token's claims in the create handler. Write tests with app.Test for a good login, a bad password, a missing header, an expired token and the open route. Run go vet, go test with -race, golangci-lint at the version the lint workflow pins, and gosec, then start the app and curl the login and the protected route, and open a pull request that shows the output.",
  "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 `handlers/auth.go`, `handlers/auth_test.go` and `handlers/handlers_test.go`, changes `app.go` for the auth group and the middleware on `POST /users`, changes `go.mod` and `go.sum` for the two modules, and adds the two environment variables to the `Makefile`'s `run-local` target and the `README`. The description shows the `curl` to `/api/v1/auth/login`, the token it returned, the `201` from `POST /users` with it and the `400` and `401` without and with a bad one, plus the five test names from `go test -v`.

The run's record keeps `go vet`, the race-enabled test run, the lint and `gosec` runs at the versions the workflows pin, and the `curl` calls, so a reviewer can see the middleware reject and accept real requests rather than read that it would.

Before merging, a reviewer decides where real credentials will live, whether the token lifetime should be shorter, and whether the `Dockerfile`'s `ENTRYPOINT` should read the secret from a file rather than the environment. The agent keeps the in-memory database and the `User` model exactly as they are.

## Run it on every Linear issue labelled auth

A team that plans API work in Linear makes an automation on the Project with a `linear` trigger on issues given the `auth` label and this prompt. The trigger appends the issue after the prompt, so an issue that says "protect the delete route too" reaches the agent as written, and each firing starts one run that opens its own pull request linking the issue.

A team without Linear starts the run from its own server with `POST /o/{org}/sessions`, the repository in `resources` and a `request_id` that is a UUID v5 of the ticket id, so a retry returns the run already started. Each run reads its own `app.go`, so a service that already has a user model with password hashes gets the login written against it instead of against environment variables.

## Why run it on Rush?

JWT code written without running it fails in ways that only show with a real request: a v2 middleware imported into a v3 app that does not compile, a middleware registered on the group before the login route so nobody can ever log in, an algorithm left unpinned, or a `400` where the client expected `401`. The agent builds and runs the real app in its own sandbox, sends the real `curl` sequence and reads the real status codes before the pull request exists.

The commands stay with the run, from `go get` to `gosec`, and the pull request cites them. Several Fiber services at once cost the same attention as one, because each run has its own machine, its own record and its own pull request.

## Frequently asked questions

### Why gofiber/contrib/v3/jwt and not a generic JWT middleware?

Because this app is on Fiber v3, whose handlers take `fiber.Ctx` by value, and the contrib module is the one written against that API and tested with v3.6.1 in its own `go.mod`. It also handles the `WWW-Authenticate` challenge, the `crit` header check and algorithm pinning, which a hand-written middleware would have to get right on its own.

### Where do the credentials come from?

In this boilerplate, from `JWT_LOGIN_PASSWORD`, because `models/user.go` has a `Name` and nothing else and the database is a slice. The pull request marks the comparison as the one function to replace with a lookup in your user store and a `bcrypt` check; the token issuing, the middleware and the tests stay as they are when you do.

### Why is a missing header a 400 and a bad token a 401?

That is the middleware's own behaviour and the agent keeps it: no credentials at all is a malformed request to a protected route, a token that fails to verify is an authentication failure. Both answers carry a `WWW-Authenticate: Bearer` challenge, and both are asserted in the tests so a future upgrade that changes them is caught.

### Will it slow the lint or security workflows?

No. The agent runs golangci-lint at v2.14.0 with `--tests=false`, as `lint.yml` does, and `gosec ./...` as `security.yml` does, in the sandbox before opening the pull request, so both come back clean on the first run rather than after a round of fixes.

## More Fiber jobs

- [Run a Fiber Docker Image as a Non-Root User](https://getrush.ai/run/add-non-root-user/fiber/)
- [Add OpenAPI Docs to a Fiber App](https://getrush.ai/run/add-openapi-docs/fiber/)
- [Add Prometheus Metrics to a Fiber App](https://getrush.ai/run/add-prometheus-metrics/fiber/)

## The same job on other stacks

- [Add JWT Login to a Koa API](https://getrush.ai/run/add-jwt-authentication/koa/)
- [Add JWT Authentication to a Litestar App](https://getrush.ai/run/add-jwt-authentication/litestar/)
- [Add JWT Authentication to a Slim 4 API](https://getrush.ai/run/add-jwt-authentication/slim/)

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