javascript - Passport 认证 : user undefined after login

标签 javascript node.js express passport.js

我正在尝试对用户进行身份验证,当我尝试使用 req.logIn 成功验证后登录用户时,但它不起作用

router.post('/login', function(req, res, next) {
    passport.authenticate('login',function (cb,data) {
       //user verfication success
       if(data){
          req.logIn({user:"shamon"},function(err,result){
              console.log("result",result,err)
              res.send('login success');
          });
       }
    })(req,res,next);
});

这个 console.log("result",result,err) 给了我 undefined,undefined

当我在登录后登录req.user时,我收到了未定义的错误

更新

var LocalStrategy    = require('passport-local').Strategy

module.exports = function(passport){

    passport.use('local',new LocalStrategy({
        usernameField: 'email',
        passwordField: 'password'
    },function (username,password,done) {
         console.log('inside passport');
         return done(null,true);
    }));



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

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

}

最佳答案

只需获取我的密码本地策略的实现即可。这肯定有效,但您需要稍微修改它:

策略:

// Serialize
  passport.serializeUser(function (user, done) {
  done(null, user.id);
});
// Deserialize
passport.deserializeUser(function (id, done) {
  User.findById(id, function (err, user) {
      done(err, user);
  });
});


passport.use('local-login', new LocalStrategy({
    // by default, local strategy uses username and password, we will override with email
    usernameField : 'email',
    passwordField : 'password',
    passReqToCallback : true // allows us to pass back the entire request to the callback
},
function(req, email, password, done) { // callback with email and password from our form
    // find a user whose email is the same as the forms email
    // we are checking to see if the user trying to login already exists
    User.findOne({ 'local.email' :  email }, function(err, user) {
        // if there are any errors, return the error before anything else
        if (err)
            return done(err);
        // if no user is found, return the message
        if (!user)
            return done(null, false, req.flash('loginError', 'No such user found.')); // req.flash is the way to set flashdata using connect-flash
        // if the user is found but the password is wrong
        if (!user.validPassword(password))
            return done(null, false, req.flash('loginError', 'Oops! Wrong password.')); // create the loginMessage and save it to session as flashd

        // all is well, return successful user
        return done(null, user);
    });
  }
));

路线:

router.post('/login', function(req, res, next) {
  passport.authenticate('local-login', function(err, user, info) {
  if (err) {
    return next(err);
  }
  if (!user) {
    return res.send({alert: req.flash('loginError')});
  }
  req.logIn(user, function(err) {
    if (err) {
      return next(err);
    }
    return res.send({redirect: '/'});
    });
  })(req, res, next);
});

关于javascript - Passport 认证 : user undefined after login,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48204913/

相关文章:

eclipse - 如何在开发时更改源代码时在 Eclipse/Aptana Studio 中自动重新启动 Node.js 应用程序?

javascript - 在我写得很好的(我认为)express 服务器上收到 404 错误

javascript - Sequelize迁移错误: Cannot read property 'length' of undefined

javascript - Node JS 从 axios 返回数据

javascript - getJSON : how to try to get some url until success in every 0. 5 秒?

javascript - 如何发送多个文件到Github?

javascript - socket.io 和 node.js 的区别

node.js - 为什么我不能在 Mongo 管道匹配中使用 $nin、$exists 等?

javascript - 突出显示时如何缩进代码使用 highlight.js

javascript - React 渲染不必要的组件