req.isAuthenticated()使用NodeJS,Express和PassportJS返回false

时间:2015-12-23 08:50:16

标签: node.js express mongoose passport.js

req.Authenticated总是返回false,但验证输入错误的用户名和密码显示完好。

这是passportjs身份验证码

passport.js

passport.serializeUser(function(user, done){
        done(null, user.id);
    });

    passport.deserializeUser(function(id, done){
        RegisterList.findById(id, function(err, user){
            console.log('deserializing user:', user);
            done(err, user);
        });
    });

    /* Customer Login Config */
    passport.use('customer-login', new LocalStrategy({
        usernameField : 'username',
        passwordField : 'password',
        passReqToCallback : true
    },
    function(req, username, password, done){
        process.nextTick(function(){
            RegisterList.findOne({'mobile_no' : username}, function(err, user){
                if(err)
                    return done(err);
                if(!user)
                    return done(null, false, req.flash('loginMessage', 'No User Found'));
                if(user.password != password){
                    return done(null, false, req.flash('loginMessage', 'Invalid password'));
                }
                return done(null, user);
            });
        });
    }));

这是我的路线代码 路由

module.exports = function(router, passport){

    router.post('/login', passport.authenticate('customer-login', {
        successRedirect: '/home',
        failureRedirect: '/login',
        failureFlash: true
    }));

    router.get('/home', isLoggedIn, function(req, res){
        res.render('secured/customer-home', {user: req.user});
    });

};

function isLoggedIn(req, res, next){
    console.log(req.session);
    if(req.isAuthenticated()){
        return next();
    }
    res.redirect('/login');
}

帮我解决这个问题......我坚持这个

更新:

使用console.log(req.session) 我明白了 Error Image

0 个答案:

没有答案