Passport authentication help?

when I run my server I got that beautiful error :expressionless:

Error: Failed to serialize user into session

and here is my passport local setup code.



passport.use(new passportLocal({usernameField: 'email'},(email,password,done) => {
    console.log(email,password);
    usermodel.findOne({email: email},(err,user_obj) => {
        console.log("user object get by database",user_obj)
        if(err) throw new Error(err);
        if(!user_obj) {
            return done(null,false,{message: "There is no user with that ID."});
        }
        else if(user_obj) {
           
            bcrypt.compare(password,user_obj.password,(err,varifiedUser) => {
                if(err) throw new Error(err);
                else if(varifiedUser) {
                    return done(null,varifiedUser);
                }
                else {
                    return done(null,false,{message: "Incorrect password!"});
                }
            })
        }
    })
}))

passport.serializeUser((user,done) => {
    console.log(user)
    done(null,user.id);
})

passport.deserializeUser((user,done) => {
    usermodel.findById(user.id,(err, id) => {
        if(!err) {
            done(null,user);
        }
        else {
            done(err);
        }
    })
})



router.post("/login",passport.authenticate('local',{
    successRedirect: '/auth/quiz',
    failureRedirect: '/auth/login'
}))

I am very frustrated I am search that error alot but I can’t solve that bustard I don’t understand where the problem lies.

Hello there,

Just a quick scan of your code:

console.log(user)
    done(null,user.id);

Are you sure user has a property id and not _id?

wait I will try that…

Not working still got that error :pensive:

Have you tried passing the user_obj to the callback, instead of verifiedUser?

Are you talking about serializeUser callback?

Yup bro it’s work :smiley: now I understand where is the problem lies. I was pass the true value instead of user object.

1 Like