All jobs

Add Login and Authentication to a CakePHP App

Yes. An agent adds login to a CakePHP 5 app with the authentication plugin: a users table, hashed passwords, login and logout, and tests, in one pull request.

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 starts from what the app already has. In an app created from the cakephp/app skeleton, composer.json requires cakephp/cakephp 5.4.* and cakephp/migrations, with cakephp/bake and PHPUnit as dev dependencies, and no authentication package. src/Application.php builds the middleware queue in a fixed order (error handler, HostHeaderMiddleware, assets, routing, body parser, CSRF protection), and its bootstrap() registers a table locator with allowFallbackClass(false), so every table the app touches needs a real class. src/Model/Table and src/Model/Entity hold only .gitkeep, there is no config/Migrations directory, AppController loads the Flash component and nothing else, and config/routes.php connects / and /pages/* to PagesController::display before fallbacks().

It runs composer require cakephp/authentication, adds the plugin to config/plugins.php beside DebugKit, Bake and Migrations, and reads the installed version under vendor/cakephp/authentication before writing any configuration, because how an identifier is attached to an authenticator differs between major versions of the plugin. It then bakes a migration for a users table (a unique email, a password column wide enough for a hash, created and modified) into config/Migrations, bakes UsersTable and the User entity, adds a _setPassword mutator that hashes with DefaultPasswordHasher, and lists password among the entity's hidden fields so it never reaches an array or JSON dump.

Application then implements AuthenticationServiceProviderInterface. Its getAuthenticationService() configures the session and form authenticators against the email and password fields and names the login route as the redirect for unauthenticated requests, and middleware() adds AuthenticationMiddleware after routing and the body parser. AppController::initialize() loads the Authentication.Authentication component, which makes every action require a signed-in user; PagesController allows display so the home page stays public, and a new UsersController gets login, logout and add, with login and add open to guests. The agent writes templates/Users/login.php and add.php with FormHelper, so each form carries the token the existing CSRF middleware expects, and puts Login and Logout links into the top-nav-links block of templates/layout/default.php.

Tests decide whether the work is finished. tests/TestCase/ApplicationTest.php asserts the first four middleware by position, so the new one goes after them and the test gains a line for it. The agent bakes tests/Fixture/UsersFixture.php and writes tests/TestCase/Controller/UsersControllerTest.php with IntegrationTestTrait: a wrong password renders the form again, a right one redirects and stores the identity in the session, a guest asking for a protected action lands on the login page with the target in the query string, logout clears the session, and a redirect value pointing at another host does not send the user there. Each POST calls enableCsrfToken() the way PagesControllerTest::testCsrfAppliedOk does. tests/bootstrap.php already runs Migrator, so the test schema comes from the new migration. The agent runs composer check, which is PHPUnit and then phpcs with the CakePHP standard over src/ and tests/, and PHPStan at the level 8 that phpstan.neon sets.

Start it with one API call

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

{
  "agent": "claude",
  "input": "Add login to this CakePHP 5 app with the cakephp/authentication plugin. Read composer.json, config/plugins.php, config/routes.php, src/Application.php, src/Controller/AppController.php, src/Controller/PagesController.php, templates/layout/default.php, tests/bootstrap.php, tests/TestCase/ApplicationTest.php and tests/TestCase/Controller/PagesControllerTest.php first. Require the plugin with Composer and read its installed version before configuring it. Add a migration under config/Migrations for a users table with a unique email and a password hash, bake the table, entity and fixture, hash passwords in the entity with DefaultPasswordHasher and hide the password field. Implement AuthenticationServiceProviderInterface in Application with session and form authenticators on email and password, add AuthenticationMiddleware after routing and the body parser, load the Authentication component in AppController, keep PagesController::display public, and add a UsersController with login, logout and add plus their templates and nav links. Extend ApplicationTest for the new middleware and write UsersControllerTest covering a wrong password, a right one, a guest redirected to login with the target kept, logout, and a redirect value on another host. Run composer check and PHPStan at the level in phpstan.neon, paste their output into a pull request, and say in it whether sign-up should stay open. Change no existing route.",
  "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 changes composer.json, config/plugins.php, src/Application.php, src/Controller/AppController.php, src/Controller/PagesController.php, templates/layout/default.php and tests/TestCase/ApplicationTest.php, and adds the migration, UsersTable, the User entity, UsersController, two templates, the fixture and the controller test. Its description carries the PHPUnit, phpcs and PHPStan output from the sandbox and the version of the plugin Composer chose.

In a repository that keeps the skeleton's .github/workflows/ci.yml, the testsuite job runs the branch on PHP 8.2 with the lowest versions the constraints allow and on PHP 8.5 with the highest, against SQLite through DATABASE_TEST_URL, and the coding-standard job runs phpcs and PHPStan. The constraint the agent writes for the plugin therefore has to hold at both ends, which is why it reads the lowest version Composer would accept before relying on a method.

Before merging, a reviewer decides three things the code cannot: whether add stays open to guests or becomes an invitation-only action, how long a session lasts (the Session block in config/app.php says only 'defaults' => 'php'), and whether any page besides the home page should be public. The existing six PagesControllerTest tests are a quick check that nothing public was closed by accident.

Run it on every new CakePHP app

A team that starts each client project from the skeleton sends this body once per new repository to 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 and never a second pull request. Each run reads its own app: one that already has a users table gets the plugin wired to the existing columns and no new migration, and one that already requires the plugin gets its configuration reviewed against the tests instead of rewritten.

Give the key a webhook_url on the console's Developers page and each run reports run.completed with the agent's final answer, which links the pull request, in result, or run.failed with the session to read when it could not finish.

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

Why run it on Rush?

Login is a change where a missed line stays invisible until someone finds it: an action left public, a password stored as typed, a redirect that follows any URL. The agent works in a sandbox of its own with PHP, Composer and SQLite, installing the intl, mbstring and pdo_sqlite extensions the CI workflow names if they are missing, so it can run the migration, start bin/cake server, sign in through the form in the sandbox's browser and run the whole suite. The commands and their output are kept with the run, and the pull request quotes them.

That record is what makes the review short. A reviewer reads which actions the tests prove are closed to guests and which are open, sees the hashed value in the fixture rather than a plain one, and spends their time on the product questions in the description. Several apps can get the same change at once, each in its own sandbox with its own database file.

Frequently asked questions

Does the home page still work without signing in?

Yes. PagesController allows its display action for guests, so / and /pages/* stay public, and the existing PagesControllerTest keeps passing as proof. Every other action requires a signed-in user once AppController loads the component, which is the safer default for controllers added later.

How are passwords stored?

As hashes. The User entity's _setPassword mutator runs DefaultPasswordHasher whenever a password is assigned, and password is a hidden field, so it is left out when a user is turned into an array or JSON. The controller test asserts that the stored value differs from the submitted one.

Will it break the CSRF protection the app already has?

No. CsrfProtectionMiddleware stays in the queue where it is, and the login and sign-up templates are built with FormHelper, which adds the token. PagesControllerTest::testCsrfAppliedError still expects a 403 for a POST without one, and the new tests enable the token explicitly.

Can it protect a JSON API with tokens instead of a login form?

Yes, as a different request. The same plugin has token and JWT authenticators; say which routes are the API and how clients get a token, and the agent configures those authenticators for that scope and tests them with an Authorization header. This page's request sets up the session and form flow only.

What if my app already has a users table?

Say so in the prompt, or let the agent find it: it reads config/Migrations and the table classes before baking anything. With an existing table it maps the authenticators to your column names, adds no migration, and reports whether the stored passwords are in a format DefaultPasswordHasher can verify.

More CakePHP jobs

More jobs like this: Hand off a long task

Keep reading