Adv.Node & Express - Logout test fails. fCC-solution fails as well

SOLVED: once again, I was able to “find” (i.e: have ChatGPT find) the solution to this issue as I was about to post this, but I’m still posting it in case it’s useful for anyone who comes across the same problem

Tell us what’s happening:

My solution looks exactly as it should

  app.route("/logout").get((req,res)=>{
      req.logout();
      res.redirect("/");
  })

but test /logout should redirect to the home page keeps failing, even though test req.logout() should be called in your /logout route passes.

Troubleshooting:
If I copy the entire solution code provided by fCC ( found at Advanced Node and Express ), the result is the same. Furthermore, if I skip to the next lesson and copypaste the fCC-provided solution for that lesson, all the tests pass, except for, once again, “Logout should work”, so I have no idea where the issue is.

Solution:

So I pasted the entire code for server.js, package.json and index.pug into ChatGPT, and it wasn’t able to find the root of the issue either, until I also pasted the error message from my VS Code terminal: Error: req#logout requires a callback function

NOW, if you’ve used ChatGPT for some time, you know it’s not perfect and that it likes to bull5h1t users a lot, so the following explanation should be taken with a grain of salt and properly researched or verified by someone from fCC before being taken as gospel, but I think it’s probably accurate, and the tests did pass after using the code it provided.

The error message “Error: req#logout requires a callback function” indicates that the req.logout() function requires a callback function as an argument. This is a requirement in some versions of the express-session middleware.

To resolve this issue, you need to provide a callback function to the req.logout() function. You can provide a simple callback function that handles any potential errors or simply ignore the callback by passing null. Here’s how you can adjust your /logout route:

app.route("/logout").get((req, res) => {
    req.logout(() => {
        res.redirect("/");
    });
});

By adding a callback function to req.logout(), you satisfy the requirement, and the error should no longer occur. This callback function can be empty if you don’t need to perform any specific actions after logging out.

You must have updated the dependencies.

https://github.com/jaredhanson/passport/blob/master/CHANGELOG.md#060---2022-05-20

req#logout() is now an asynchronous function and requires a callback function as the last argument.

I would assume it works just fine with the version in the boilerplate dependencies.

1 Like

I honstly have no idea at this point; I’m comparing my package.json file to the one on the fCC repo and the only difference is socket.io=“^2.5.0” on mine and socket.io=“~2.3.0” on the repo. All the other dependencies are the same, including express-session which is set to ~1.17.1 both on my package.json as well as on the original repo.

Also, while I’m at it, I’m currently struggling with one of the next lessons, " Implementation of Social Authentication".

ACTUALLY NEVERMIND, I have now blurred this part because I tried pasting the fCC code for SERVER.JS and that did, so I’ll compare that to my server.js file and figure out what the issue was, so I don’t want to waste your time. Still, thanks for all your help Lasse. As you can probably tell this, this unit is driving me nuts.

I have to create the OAuth app on GitHub but I’m working locally and I just want to know if it’s the same process as for people who use Replit or Gitpod.
More specifically, is it OK for me to set the

“Homepage URL” to http://localhost:3000/
and
“Authorization Callback URL” to http://localhost:3000/auth/github/callback

I can’t get any of the tests to pass, not even when copying the fCC-provided code.

I get :

Route /auth/github should be correct.
Route /auth/github/callback should be correct.
// tests completed
// console output
[Error: AssertionError: Target cannot be null or undefined.]
[Error: AssertionError: Target cannot be null or undefined.]

(I know that maybe you can’t find the issue without looking at the whole code. For now, I just want to confirm if it’s OK to use localhost when setting up those URLs)

The passport version in the boilerplate is 0.4.1 and the callback API update was in 0.6.0

I would expect it to ignore the callback to req.logout() using 0.4.1 as that version of the function doesn’t take any arguments.