Dognation project - user not appended to req

Dognation - authentication passport excercise

Hello,

My problem relates to the Dognation project. It seems I get succesfully authenticated but somehow the policy has not appended the user to the request object. This is the point where it goes wrong. ‘Authenticated!’ gets logged to the console, but when redirected to the homepage, still ‘guest’ is mentioned ! (instead of the user name)

in user.routes.js

router.post(
“/login”,
passport.authenticate(“local”, { failureRedirect: “/login” }),
(req, res) => {
console.log(“authenticated!”);
res.redirect(“…/”);
}
);

In app.js

app.get(“/”, (req, res) => {
//console.log(req);
//console.log(‘req.user :’ + req.user);
//console.log(‘user :’ + user);

    const user = req.user || "Guest";
    console.log('user' + user);
    res.render("home", { user });

});

I have seen a lot of comments and questions about this project, but still I don’t think someone posted a fully working solution.

Can the Codecademy staff please have a look at this? Implenting authentication is an important part of skillset so I would like them to take their responsibility on this.

Thank you

@system3210230902 - I was having a similar issue and I tore the code apart and redid the project at least 3 times…finally I discovered that I deserializeUser was not getting called for some reason. I ran across a post on github that helped me out: passport issues. I didn’t actually use the log dumps described in the post (although that could prove helpful in the future), but it made me wonder if my browser was blocking the cookie from behaving properly. Switching the project from Safari over to Chrome solved the issue and it works properly and logs in the user as expected.

Here is a GIST with the working code. Let me know if this resolves it for you. Would also love to hear how our code may differ.

Cheers!

Thank you for your help.

One of the main differences when comparing our codes is the setup of the sesion. This is my code snippet

app.use(
  session({
    secret: "D53gxl41G",
    cookie: { maxAge: 1000 * 60 * 48  },
    saveUninitialized: false,
    resave: false,
    sameSite: "none",
    secure: true
  })
)

You have sameSite inside the cookie object. I can give it a try to do it like you

I am already on Chrome - do you actually see the word ‘guest’ replaced with your ‘username’ ?

I tried your session configuration.

app.use(
  session({
    secret: "oiajsfoi234",
    cookie: {
      maxAge: 1000 * 60 * 24,
      sameSite: "none",
      secure: true,
    },
    saveUninitialized: false,
    resave: false,
  })
);

It seems to deviate from the instructions, but now it works !

Thank you very much,

Johan (Belgium)

Glad it is working for you now! This was a frustrating project and the instructions were very unclear. In looking at the session options in the documentation it describes the options in better detail. Express Session - It was here that I realized part of the problem may have been in where I had placed options. Hopefully Codeacademy will update that project based on community feedback.

1 Like