Passport req.isAuthenticated() always returning false,and req.user() is undefined
Asked Answered
Q

1

0

I am using passport-facebook and express-session for storing sessions of users on the server side. The session is getting stored in mongo,and cookie is also getting set in browser,but req.isAuthenticated() is returning false,and req.user() is undefined.

passport.deserializeUser is also working fine.

Any idea on why this might be happening?

Quanta answered 26/2, 2018 at 15:55 Comment(2)
Difficult to say without any code! Have you setup the passport middleware?Aam
Your passport success redirect "config.facebookAuth.redirect", this does not exist in your config fileAam
Q
1

For me,it was a CORS issue.Though the cookie was properly set in browser by passport,but it was not getting sent back by the subsequent axios requests.So both req.isAuthenticated() and req.user() were not working.

To make it work,I had to set the following in client side:

axios.defaults.withCredentials = true;

and following in server side(NodeJS + express):

  app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "http://localhost:3000");
  res.header("Access-Control-Allow-Credentials",true);
  next();
});
Quanta answered 27/2, 2018 at 4:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.