All jobs

Add Account Deletion to an AdonisJS API

Yes. An agent adds a DELETE account endpoint to your AdonisJS API that checks the password, revokes tokens and removes the user, with Japa tests, and opens a PR.

Run this on RushOpens the console with this prompt ready to send, in a Project that has your repository.

What the agent does

The agent reads how accounts are created and authenticated before it writes the way to remove one. In the api starter kit from adonisjs/starter-kits, start/routes.ts groups /api/v1 into auth/signup and auth/login, open to anyone, and account/profile and account/logout, behind middleware.auth(), each pointing at a controller through the generated controllers registry in .adonisjs/server/controllers.ts. NewAccountController.store validates with signupValidator, creates the user and returns a token with User.accessTokens.create; AccessTokensController.destroy deletes the current token by its identifier; ProfileController.show returns the user through UserTransformer, which picks id, fullName, email, timestamps and initials. app/models/user.ts composes the generated UserSchema from database/schema.ts, a file the migration command regenerates and marks do-not-edit, with withAuthFinder(hash) and a DbAccessTokensProvider. config/auth.ts makes the api tokens guard the default beside a web session guard. The auth_access_tokens migration declares tokenable_id with onDelete('CASCADE'), and config/database.ts runs on SQLite through better-sqlite3 at tmp/db.sqlite3. tests/bootstrap.ts registers Japa's assert, API client, auth and session API clients and database assertions, starts the HTTP server for the functional suite, and has empty setup and teardown hooks. providers/api_provider.ts wraps every serialized response under data.

The agent adds deleteAccountValidator to app/validators/user.ts, a password string, and app/controllers/delete_account_controller.ts with one destroy method: it validates the body, takes the user from auth.getUserOrFail(), checks the password with User.verifyCredentials(user.email, password) so a wrong password fails the same way a bad login does, and then, inside a database transaction, deletes the user's rows in auth_access_tokens and the user itself, answering with response.noContent(). The token delete is explicit rather than trusting the migration's cascade, because SQLite only enforces foreign keys when the connection turns the pragma on and a Postgres swap later should not change behaviour. The route is router.delete('account', [controllers.DeleteAccount, 'destroy']) inside the authenticated account group, and the agent runs the app once so the indexEntities hook in adonisrc.ts regenerates the controllers registry, which is committed.

Tests go in tests/functional/account/delete.spec.ts. The agent fills the empty setup hook in tests/bootstrap.ts with testUtils.db().migrate() and adds a per-suite truncate so the functional suite starts on a fresh schema, which the kit leaves for the developer. The cases: an authenticated user with the right password gets 204 and assert.dbMissing confirms both the users row and the auth_access_tokens rows are gone; a wrong password gets the same status the login route returns for bad credentials and the user is still there; and a request without a token gets 401. Each uses loginAs(user) from the auth API client so the tokens guard issues a real token.

The agent runs pnpm install, which builds better-sqlite3 under the kit's allowScripts, then node ace migration:run, pnpm test, pnpm lint and pnpm typecheck in its sandbox. The README.md feature list gains the endpoint. Nothing in the web guard, sessions or Shield changes.

Start it with one API call

POST https://api.phoenix.so/o/{org}/sessions with this body:

{
  "agent": "claude",
  "input": "Add account deletion to this AdonisJS API. Read start/routes.ts, app/controllers/*.ts, app/validators/user.ts, app/models/user.ts, database/schema.ts, database/migrations/*.ts, config/auth.ts, config/database.ts, providers/api_provider.ts, adonisrc.ts and tests/bootstrap.ts first. Add a deleteAccountValidator with a password field, and a DeleteAccountController whose destroy method validates the body, takes the user from auth.getUserOrFail, verifies the password with User.verifyCredentials, and inside a transaction deletes the user's auth_access_tokens rows explicitly and then the user, answering 204. Register DELETE account in the authenticated account group through the generated controllers registry and commit the regenerated registry. Fill the empty setup hook in tests/bootstrap.ts with a migration and a truncate for the functional suite, and write tests/functional/account/delete.spec.ts covering success with dbMissing on both tables, a wrong password leaving the user in place, and no token giving 401, using loginAs. Add the endpoint to the README feature list. Run pnpm install, node ace migration:run, pnpm test, pnpm lint and pnpm typecheck, and open a 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

The pull request adds app/controllers/delete_account_controller.ts and tests/functional/account/delete.spec.ts, changes app/validators/user.ts, start/routes.ts, tests/bootstrap.ts, README.md and the regenerated .adonisjs/server/controllers.ts. Its description pastes the Japa output with the three cases named, the lint and type-check results, and a curl of the new route from the sandbox showing the 204 and the 401.

Before merging, a reviewer decides whether deletion should be delayed with a grace period or a confirmation email rather than immediate, whether any table added later with a foreign key to users needs its own line in the transaction, and whether the web session guard, which this API ships but does not use by default, should log the session out in the same request.

Run it on every API repository

A team that starts each service from the same kit sends this body once per repository from its own server, each a POST /o/{org}/sessions with that repository in resources and a request_id that is a UUID v5 of the repository's full name, so a retried call returns the run already started. Each run reads its own schema: a service with posts or uploads owned by the user gets those rows handled in the same transaction, and one on Postgres keeps the explicit token delete for the same reason.

Once the endpoint exists, an automation on the Project with a github trigger on pull_request.opened and the prompt "if this pull request adds a migration with a foreign key to users, comment on whether delete_account_controller.ts needs to remove those rows too" starts one run per pull request. The trigger adds the pull request after the prompt, and only work by an owner, member or collaborator fires it.

The use case this job follows, with its automation and trigger: Hand off a long task.

Why run it on Rush?

Deleting an account is the endpoint that is easy to write wrong in a way nobody notices until a user asks: a password check skipped, tokens left valid after the row is gone, a cascade that works on one database and not another. The agent reads the kit's auth setup, writes the deletion as a transaction with every step explicit, and proves each case with the test tooling the kit already registers.

The Japa run, the lint and the type check happen in the run's own sandbox with a real SQLite database, and their output stays with the run for the reviewer. The same request runs across every service a team built from the kit, each in its own sandbox.

Frequently asked questions

Why ask for the password when the request already carries a token?

A token proves a session, not a person: a leaked token or an unattended laptop should not be enough to erase an account. User.verifyCredentials is what the login route already uses, so a wrong password fails with the same error shape the client knows.

Why delete tokens explicitly when the migration has a cascade?

The kit runs on SQLite, which enforces foreign keys only when the connection enables the pragma, and the config/database.ts comments show the same schema is meant to move to Postgres or MySQL. Deleting the auth_access_tokens rows in the transaction gives the same result on every driver and the test asserts it.

What does the test change in tests/bootstrap.ts do?

The kit ships setup and teardown as empty arrays. The agent adds a migration in setup and a truncate for the functional suite so each run starts from the schema in database/migrations, which is also what the new dbMissing assertions rely on.

Does the response go through the data wrapper?

No. providers/api_provider.ts wraps serialized bodies under data, but a successful deletion answers 204 with no body, so the controller calls response.noContent() and nothing is serialized. Errors keep the format the exception handler already produces.

More AdonisJS jobs

The same job on other stacks

Keep reading