使Passport.js验证用户身份时遇到问题 [英] Problems getting Passport.js to authenticate user

查看:79
本文介绍了使Passport.js验证用户身份时遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法获得Passport来对我的用户进行身份验证.由于某种原因,passport.authenticate方法始终会失败.我不明白的是,如果我在通电话之前添加了中间件,则可以通过req.user访问用户数据.

I am unable to get Passport to authenticate my user. For some reason the passport.authenticate method always fails. What I don't understand is that if I add middleware before the passport call, the user data is accessible via req.user.

有什么想法说明为什么password.authenticate失败?

Any ideas to why passport.authenticate is failing?

app.get('/app'
  // MY USER SHOWS UP HERE
  , function(req, res, next) {
    console.log("app.get('/app')", req.user);  
    next();
  }

  // DOESN'T MAKE IT PAST HERE
  , passport.authenticate('local', { failureRedirect: '/login', failureFlash: true })

  // NEVER MAKES IT HERE
  , function(req, res) {   
    console.log('FTW!!!');
    res.render('../../client/app/app')
  }
);

验证码

passport.use(new LocalStrategy({ usernameField: 'email', passwordField: 'password' },
  function(email, password, done) {
    console.log("Authenticating user: ", email, password);
    User.findOne({ email: email }, function (err, user) {
      if (err)   return done(err);
      if (!user) return done(null, false, { message: 'Invalid Email' });

      return user.authenticate(password, function(err, valid) {
        if (err)    return done(err);
        if (!valid) return done(null, false, { message: 'Invalid Password' });
        return done(null, user);
      });
    });
  }
));

passport.serializeUser(function(user, done) {
  console.log('Serializing: ', user);
  done(null, user.id);
});

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

注册并重定向:找到用户后,请注意['Missing Credentials']消息

Signup and redirect: Note the ['Missing Credentials'] message after it has found the user

// SIGN UP
Authenticating user:  cacky23@acme.org.com foobar
User.authenticate(): Comparing...  // user.authenticate()
Compare Complete... true           // bcrypt compare
Serializing:  { email: 'cacky23@acme.org.com',
  password: '$2a$10$TJcvr4wtgs6DFaNnyQSLt.v5GLbt8PIi.oOlgqZpvghveKEPEcroW',
  _id: 52160e247d0aa8e328000001,
  __v: 0,
  createdAt: Thu Aug 22 2013 07:12:04 GMT-0600 (MDT) }
POST /users 302 185ms - 64b
Deserializing:  52160e247d0aa8e328000001
Found User:  { email: 'cacky23@acme.org.com',
  password: '$2a$10$TJcvr4wtgs6DFaNnyQSLt.v5GLbt8PIi.oOlgqZpvghveKEPEcroW',
  _id: 52160e247d0aa8e328000001,
  __v: 0,
  createdAt: Thu Aug 22 2013 07:12:04 GMT-0600 (MDT) }

// MY TEST MIDDLEWARE 
app.get('/app') { email: 'cacky23@acme.org.com',
  password: '$2a$10$TJcvr4wtgs6DFaNnyQSLt.v5GLbt8PIi.oOlgqZpvghveKEPEcroW',
  _id: 52160e247d0aa8e328000001,
  __v: 0,
  createdAt: Thu Aug 22 2013 07:12:04 GMT-0600 (MDT) }
GET /app 302 6ms - 68b
Deserializing:  52160e247d0aa8e328000001
Found User:  { email: 'cacky23@acme.org.com',
  password: '$2a$10$TJcvr4wtgs6DFaNnyQSLt.v5GLbt8PIi.oOlgqZpvghveKEPEcroW',
  _id: 52160e247d0aa8e328000001,
  __v: 0,
  createdAt: Thu Aug 22 2013 07:12:04 GMT-0600 (MDT) }
[ 'Missing credentials' ]
GET /login 200 85ms - 3.65kb

推荐答案

护照认证如何失败?它会引发错误,还是应用程序崩溃?

How does passport authentication fail? Does it throw an error, or does the app crash?

尝试使用req.isAuthenticated()检查用户是否已登录. 当http帖子请求中提供登录凭据时,我仅在登录时使用passport.authenticate.

Try to use req.isAuthenticated() to check whether the user is logged in. I use passport.authenticate only while login when login credentials are provided in a http post request.

因此,在/app上仅使用

So on /app use only

if(req.isAuthenticated()) {
    console.log('user logged in', req.user);
    next();
}
else {
   console.log('user not logged in');
}

在/app/login上,您可以使用已有的代码. 现在,您可能已经登录了.尝试删除cookie.之后,它可能不再将用户对象记录在console.log(req.user)上.

And on /app/login you can use the code you already have. Right now you're probably already logged in. Try to delete the cookie. After that it probably doesn't log the user object on console.log(req.user) anymore.

此外,所有代码看起来都是正确的.因此,如果无法正常工作,请尝试精简代码. 我们不知道您在user.authenticate中拥有什么代码.该功能有效吗?

Besides that all your code looks correct. So if isn't working, try to strip down your code. We don't know what code you have in user.authenticate. Is that function working?

致谢

这篇关于使Passport.js验证用户身份时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆