passport身份验证:登录后用户未定义

时间:2018-01-11 10:31:15

标签: 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);
    });

}

1 个答案:

答案 0 :(得分:0)

抓住我的密码本地策略实施。这是肯定的,但您需要稍微修改它:

战略:

// 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);
});
相关问题