All jobs

Fix "Error: req#logout requires a callback function" in Passport 0.6 and later

Yes. An agent gives every req.logout() call a callback, moves the redirect inside it, and shows the logout route answering 302 again on a fresh install.

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

What the agent does

Passport 0.6.0 changed req.logout() from a synchronous call into one that takes a callback. In 0.7.0 the check is in lib/http/request.js: when the argument is not a function, req.logout throws Error: req#logout requires a callback function. A route written against Passport 0.3 or 0.4 that calls req.logout() and then redirects keeps working until the day the installed Passport crosses 0.6, and then every visit to the logout route becomes a 500.

DanialK/PassportJS-Authentication, the source of a widely copied Passport tutorial, shows how that day arrives without anyone editing the code. Its package.json pins express to 3.1.0 and mongoose to ~3.6.4 but declares passport, passport-local, passport-facebook, passport-google-oauth, jade and connect-flash as *. There is no lockfile, so npm install today resolves passport to 0.7.0, passport-local to 1.0.0, passport-facebook to 3.0.0 and jade to 1.11.0 next to Express 3.1.0 and Mongoose 3.6.20. In config/routes.js the last route is app.get('/logout', function(req, res){ req.logout(); res.redirect('/login'); }). The signup route a few lines above already calls req.login(user, function(err){ ... }) with a callback, because req.login has required one for much longer.

The agent reproduces before it edits. node app.js needs a MongoDB on localhost, and without one Mongoose 3.6 raises an unhandled connect error that exits the process, so the agent builds a small harness instead: an Express 3 app with express.cookieParser(), express.session({ secret: ... }), passport.initialize() and passport.session(), the fixture's own config/routes.js mounted on it, and an error handler that prints the error message. A GET /logout through that harness answers 500 with req#logout requires a callback function in the log. The same harness after the fix answers 302 to /login.

The fix is confined to the one route. The handler gains next, the redirect moves inside the callback, and an error from Passport goes to Express: req.logout(function(err){ if (err) return next(err); res.redirect('/login'); }). The agent then searches the repository for every req.logout( and req.logOut( to confirm no other call is left bare, reads app.js to confirm the session middleware runs before passport.session(), since the new logout works on req.session, and leaves Express 3, Mongoose 3.6 and the * ranges as they are: widening the change to a dependency upgrade is a different job, and the request below says so.

Start it with one API call

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

{
  "agent": "claude",
  "input": "The logout route in this Express app answers 500 with \"Error: req#logout requires a callback function\" since Passport was installed fresh. Read package.json and the lockfile if there is one, run npm install, and report the Passport version that was resolved and the version range that allowed it. Find every call to req.logout() or req.logOut() in the repository and list each one with its file and line. Reproduce the error before changing anything: if the app needs a database to start, mount the route module on a minimal Express app with the same session and Passport middleware and request the logout route, and keep the status and the error message. Change each logout call to pass a callback, move the redirect or response into that callback, and pass any error to next. Do not change the Passport version, the other dependencies, or the Express version. Run the same reproduction again and report the new status and Location header. Open a pull request with the before and after status, the resolved versions, and the list of changed calls.",
  "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

A pull request that changes one route handler per logout call, typically a single file. In the fixture above the diff is three lines in config/routes.js: the handler takes next, req.logout gets a callback, and the redirect moves into it. The description lists the Passport version npm install resolved and the * range in package.json that let it float, and shows the harness run twice: 500 with the error text before, 302 with Location: /login after.

What a reviewer checks is that nothing else moved. The Passport version is unchanged, the session middleware still runs before passport.session(), and every req.logout( in the repository now has a callback, which the pull request proves with the search output rather than a sentence. If the app also calls req.login or req.logIn without a callback, that is listed as a separate finding, since it throws its own req#login requires a callback function.

The pull request does not pin Passport back to 0.5. A pin would make the route work again without changing it, but the * ranges in this package.json would keep every other dependency floating, and the fix is three lines.

Run it on every dependency bump pull request

A floating range like "passport": "*" breaks on the day of an install, not on a commit, so the useful trigger is the install. Give the Project an automation with a github trigger on pull_request.opened and a prompt that tells the agent to run npm install on the branch, request every route that logs a user in or out through a harness like the one above, and report any that answer 500. The trigger adds the pull request to the prompt; a branch that moves Passport past 0.6 then arrives with the broken route named before anyone merges it. A pull request a bot opens fires no github trigger, so a Dependabot or Renovate bump is checked by a schedule trigger with a weekly cron whose prompt tells the agent to check the open dependency pull requests.

For a fleet of small Express services that share this tutorial's shape, start the request above once per repository from a script with an API key, and read each session's pr_url when the run.completed webhook arrives. Runs past the key's max_concurrent limit are accepted and wait as queued, so the script needs no retry loop.

The use case this job follows, with its automation and trigger: Fix a bug from an alert.

Why run it on Rush?

The three-line fix is not the work. The work is proving, without a MongoDB and without a browser, that the logout route fails before the change and redirects after it, on the exact Passport an npm install resolves today. An agent in its own sandbox installs the dependencies as a new clone would, writes the harness, runs it twice, and leaves both runs in the session's record, so the pull request arrives with its evidence attached.

It also starts from the state your users are in. On a laptop that has had node_modules since 2019, Passport is still 0.4 and the route has never failed. A sandbox has no node_modules: the install resolves what a fresh checkout gets, which is the only place this bug exists.

Frequently asked questions

Why does Passport 0.6 need a callback for logout?

From 0.6.0 (May 2022) req.logout takes a callback and reports through it, and 0.7.0's lib/http/request.js throws req#logout requires a callback function when none is passed. req.login had required a callback for longer, which is why a tutorial's signup route usually already has one while its logout route does not.

Can I pin passport to 0.5 instead?

It works until something else needs a newer Passport, and in a package.json full of * ranges a pin on one package leaves every other one floating. The request above tells the agent not to change versions: the fix is one callback per call.

Does the agent upgrade Express 3 while it is there?

No. The fixture's Express 3.1.0 still installs and serves the routes, and moving it to Express 4 or 5 changes app.configure, express.session and the error handler, which is a different pull request. The agent reports what else is old; it changes only the logout calls.

My app will not start without a database. How does the agent reproduce the error?

It mounts the route module on a minimal Express app with the same cookie, session and Passport middleware, the way the fixture's config/routes.js was tested above, and requests the logout route with a plain HTTP client. The user model is loaded but never queried, so no database is needed to see the 500 or the 302.

Will it find req.logOut as well as req.logout?

Yes. Passport exposes both names, and the agent searches for both spellings and lists each call with its file and line before changing them.

More Passport jobs

More jobs like this: Fix a bug from an alert

Keep reading