Passport + Express4 +本地策略 - req.isAuthenticated返回false

时间:2016-07-13 12:13:23

标签: angularjs node.js authentication express passport.js

使用带护照的req.login后,req.user将返回该对象。当我想检查用户是否仍然登录时,req.user为空,req.isAuthenticated()返回false。使用角$ http任何想法?感谢。

passport.js

module.exports = (app) => {

// var LocalStrategy = passportLocal.Strategy;

app.use(expressSession({ secret: 'mysecret' }));
app.use(passport.initialize());
app.use(passport.session());

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

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

passport.use(
    new LocalStrategy({
        usernameField: 'email',
        passwordField: 'password'
    },
    function (email, password, done) {
            new AdminUsers({ 'email' : email })
                        .fetch()
                        .then(function(user) {
                            // if user doesn't exists
                            if (!user) return done(null, false, { message: 'User doesnt exists' });

                            // use bcrypt to encrypt password while save, and to compare.
                            bcrypt.compare(password, user.attributes.password, function (err, res) {
                          if (!res) return done(null, false, {message: 'Invalid Password'});
                        });

                      return done(null, user);
                        });
}));
}

登录电话

login: (req, res, next) => {
    passport.authenticate('local', (err, user, info) => {
      req.login(user, (err) => {
        if (err) {
          res.json(err);
        } 
        else {
            res.json({ 'redirect' : '/' });
        }
        return;
      });
    })(req, res, next);
}    

并验证

res.send(req.isAuthenticated() ? '1' : '0');

编辑:req.session在成功登录后不存储护照会话。

0 个答案:

没有答案