Open Anubhav-Nigam opened 2 years ago
Disclaimer: I started writing nodejs like a week ago and I have basic js knowledge, so my use of terms may be basic.
Now, from what I see, the issue is with your deserializer function not working properly. You need to attach the user object to the done callback, like this: passport.deserializeUser((id, done) => User.findById(id, (err, user) => done(err, user)));
so in your own case, it should be:
Model = require('your model here') // e.g. const User = require('../models/User');
passport.deserializeUser((user_id, cb) => {
Model.findById(user_id, (err, user) => cb(null, user));
});
// I personally would prefer to use user_id instead of user to avoid the id clashing with the user object.
Recap: The serializer expects an id of the currently logged-in user which it then uses to build the session for the passport library.
Also, make sure you set the appropriate libraries and initialize the express-session or express-cookie before passport session. i.e
app.use(passport.initialize());
app.use(passport.session());
Hope this helps.
req.isAuthenticated() is returning false even after authenticating with AWS cognito.
The function which I am using for adding login routes is:
And for checking if the request is authenticated I am using:
The _passport object printed inside res is:
getCognitoInstance() returns the login page url of hosted UI of cognito. validateUrlAndRedirectCognito() redirects to that url.
Since the value of '_userProperty' is 'user', req.isAuthenticated() should return true after authentication. Please help me with what is wrong in this code.