3

我正在使用具有以下配置的`feathers-passport 向节点应用程序添加身份验证:

.configure(feathersPassport({
    secret: 'some-secret',
    store: new MongoStore({db: 'demo-session'}),
    resave: false,
    saveUninitialized: false
}))

我配置护照:

var localOptions = {
    usernameField: 'email',
    passwordField: 'password'
};

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

passport.deserializeUser(function(id, done) {
  api.service('users').get(id, {}, done);
});

passport
    .use(new LocalStrategy(localOptions, function(email, password, done){
        api.service('users').authenticateLocal(email, password, done);
    }))

要进行身份验证,我有以下路由设置:

.post('/users/authenticate', function(request, response, next){
    passport.authenticate('local', function(error, user){
        //error comes back as null and user is a valid object
        request.login(user, function(error){
            if(error) return next(error);
            if(!user) return next(new Error("no user found"));
            else {
                var token = tokenIssue({id: user.id});
                response.status(201).json({token: token, id: user.id});
            }
        });
    })(request, response, next);
})

但是,会话序列化总是失败并出现标准错误: Failed to serialize user into session,这通常表明serializeUseranddeserializeUser函数不存在。除了在这种情况下,他们是。我浏览了护照来源和这里(第 265 行authenticator.js):

var stack = this._serializers;
(function pass(i, err, obj) {
// serializers use 'pass' as an error to skip processing
  if ('pass' === err) {
    err = undefined;
  }
  // an error or serialized object was obtained, done
  if (err || obj || obj === 0) { return done(err, obj); }

  var layer = stack[i];
  if (!layer) {
    return done(new Error('Failed to serialize user into session'));
  }

this._serializers打印出来,[]所以当var layer = stack[i]被调用时,检查if(!layer)总是通过,因此抛出错误。似乎护照没有发现我已经定义了序列化功能,我不确定这是护照的结果还是使用feathers-passport. 我非常仔细地遵循feathers-passport指南,我无法弄清楚到底出了什么问题。任何帮助,将不胜感激。

4

0 回答 0