【问题标题】:Problems getting Passport.js to authenticate user让 Passport.js 对用户进行身份验证时出现问题
【发布时间】:2013-08-23 06:17:57
【问题描述】:

我无法让 Passport 对我的用户进行身份验证。由于某种原因,passport.authenticate 方法总是失败。我不明白的是,如果我在护照调用之前添加中间件,则可以通过req.user 访问用户数据。

对为什么 passport.authenticate 失败有任何想法吗?

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'] 消息

// 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

【问题讨论】:

    标签: node.js express passport.js


    【解决方案1】:

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

    尝试使用 req.isAuthenticated() 来检查用户是否登录。 我仅在 http post 请求中提供登录凭据时才使用 passport.authenticate。

    所以在 /app 上只使用

    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) 上记录用户对象了。

    此外,您的所有代码看起来都是正确的。因此,如果不起作用,请尝试删除您的代码。 我们不知道您在 user.authenticate 中有什么代码。那个功能有效吗?

    问候

    【讨论】:

    • 它在中间件中失败并将用户重定向到 /login(由 failureRedirect 指定)。反序列化器方法返回的用户模型是我的自定义模型,没有 isAuthenticated 方法,但返回了一个用户。 user.authenticate 在登录时确实可以正常工作(已显示控制台日志声明)。
    • 看起来你基本上是对的,除了可能是一个错字。它应该是 req.isAuthenticated(),可以在示例 github.com/jaredhanson/passport-local/blob/master/examples/… 中看到。如果您可以编辑您的建议,我会将其标记为解决方案。
    • 抱歉打错了。我将 req.user.isAuthenticated() 更新为 req.isAuthenticated()。
    猜你喜欢
    • 2020-01-02
    • 2021-06-09
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 2019-06-01
    • 1970-01-01
    • 2018-05-03
    • 2023-04-05
    相关资源
    最近更新 更多