【问题标题】:NodeJS, Passport & Passport-LocalNodeJS, Passport & Passport-Local
【发布时间】:2016-06-25 00:17:32
【问题描述】:

所以我遇到了使用本地策略进行身份验证的问题。如果我传递了无效的凭据或任何不成功的东西,我会收到相应的错误。但是,如果他们的身份验证成功,则会出现 404 错误。

我已经挖掘了,我遇到的最好的想法是删除会话存储(这似乎是它发生的地方,同时序列化用户)。有人遇到过这样的问题吗?

这是一些代码,如果您需要任何其他代码部分,请告诉我,我很乐意提供。

我的护照配置:

var passport = require('passport'),
User = require('mongoose').model('User');

module.exports = function () {
    passport.serializeUser(function (user, done) {
        done(null, user.id);
    });

    passport.deserializeUser(function (id, done) {
        User.findOne({ _id: id }, '-password -salt -__v', function (err, user) {
            done(err, user);
        });
    });

    require('./strategies/local')();
};

我的本​​地策略配置:

var passport = require('passport'),
    LocalStrategy = require('passport-local').Strategy,
    User = require('mongoose').model('User');

module.exports = function () {
    passport.use(new LocalStrategy({
        usernameField: 'email'
    }, function (email, password, done) {
        User.findOne({email: email}, function (err, user) {
            if (err) {
                return done(err);
            }

            if(!user || !user.active || !user.authenticate(password)) {
                return done(null, false, { message: 'Authentication Failed'});
            }

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

所有其他方法都有效,我可以查询数据库以找到用户,我已经逐步将散列密码与提供的密码匹配,这一切似乎都发生了,我在 LocalStrategy 的末尾得到了一个用户(我进入决赛done(null, user)。在serializeUser()done(null, user.id) 发生了一些事情。我尝试过它,但我最终进入的内容似乎相当模糊(或者我太笨了无法理解它)所以我不知道实际发生了什么。

【问题讨论】:

  • 听起来对passport.authenticate() 的调用可能配置不正确。你能展示一下你是如何使用它的吗?
  • 我在那里没有做任何特别的事情:app.route('/login') .post(passport.authenticate('local')); 我不需要做任何重定向或任何事情,我只需要将用户对象返回到前端。一件事可能完全是巧合,最近节点加密pbkdf2Sync 函数开始抱怨不推荐在没有摘要的情况下调用它,所以我添加了它,现在我遇到了这个问题。 --edit 刚刚测试过了,还是一样的……

标签: node.js authentication passport.js passport-local


【解决方案1】:

这是您的路线设置:

app.route('/login').post(passport.authenticate('local'));

这将在身份验证失败时生成 401(“Unauthorized”),但当它成功时,接下来不会发生任何配置,因此您将得到 404。

你应该在你的路由中添加一个显式的“成功”处理程序:

app.route('/login').post( 
  passport.authenticate('local'),
  function(req, res) {
    // This will only get called when authentication succeeded.
    res.json({ user : req.user });
  }
);

【讨论】:

  • 在这种情况下,请求对象是null。无论serializeUser() 中发生了什么,都在炸毁任何数据;
  • 所以我做了更多的挖掘工作,遇到了connect-mongo。我用它来存储我的会话,在其中,我发现护照会话正在被存储......所以必须在serializeUser() 本身内部进行轰炸(可能在它调用序列化用户信息本身之后)。我正在挖掘 Passport 的 github,看看能不能找到任何东西。
  • 肯定有其他可疑的事情发生,我使用相同系统的另一个项目已经停止工作。它在本周初工作得很好。没有包更新或类似的东西......我可以在我的家用机器上再试一次,看看那里会发生什么。
  • reqnull 是没有意义的(或者你的意思是 req.user?)。您是否记录了userserializeUser() 中的内容,看看是否有意义?
  • @DaveV 您获得的变量的内容(null,用户)建议您正在将其用作回调(passport.authenticate('local', function(req, res) { ... })),即完全不同的东西(事实上,该函数的参数是err, user, info,与你得到的匹配)。应该是passport.authenticate('local'), function(req, res) { ... })(注意local后面的括号)
猜你喜欢
  • 2020-11-10
  • 2014-03-24
  • 2017-08-15
  • 2019-08-13
  • 2013-07-23
  • 2018-02-08
  • 1970-01-01
  • 1970-01-01
  • 2018-12-13
相关资源
最近更新 更多