Passport:会话序列化失败,存在序列化函数

时间:2015-10-27 17:28:06

标签: serialization passport.js feathersjs

我正在使用带有以下配置的`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而失败,这通常表示serializeUserdeserializeUser函数不存在。除了这种情况,他们是。我通过了护照来源和这里(authenticator.js的第265行):

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指南,我无法弄清楚到底出了什么问题。任何帮助将不胜感激。

0 个答案:

没有答案