保存后护照不会序列化模型
Passport wont serialize Model after saving
我正在使用 passport/SQL 构建一个基本的用户身份验证应用程序,似乎 passport 在序列化模型并将其传递给下一个请求时遇到问题。
发布到我的注册表单并成功保存用户后(在 SQL 中检查并确认行已插入数据),我想将模型传递给 Passport 以序列化并在下一页使用.
passport.use('local-signup', new LocalStrategy({
usernameField : 'email',
passwordField : 'password',
passReqToCallback : true
},
function(req, email, password, done) {
process.nextTick(function() {
new User({
localEmail: email,
localPassword: User.generateHash(password)
}).save().then(function(model) {
return done(null, model);
});
});
}));
它到达了应该序列化它的地方,但我 运行 遇到错误 Error: failed to serialize user into session
。现在这没有多大意义,因为理论上,我应该通过 return done(null, model);
在保存承诺中传递新保存的用户
passport.serializeUser(function(user, done) {
done(null, user.localEmail);
});
passport.deserializeUser(function(email, done) {
new User({localEmail: email}).fetch().then(function(user) {
done(null, user);
});
});
我是不是遗漏了什么明显的东西?任何帮助将不胜感激,谢谢!
Bookshelf 的模型属性不会作为简单的对象属性公开,它们需要一个 get()
方法才能达到(等待我们在那里拥有适当访问器的日子...)
那么,请尝试将您的 serializeUser()
更改为:
passport.serializeUser(function(user, done) {
done(null, user.get('localEmail'));
});
我正在使用 passport/SQL 构建一个基本的用户身份验证应用程序,似乎 passport 在序列化模型并将其传递给下一个请求时遇到问题。
发布到我的注册表单并成功保存用户后(在 SQL 中检查并确认行已插入数据),我想将模型传递给 Passport 以序列化并在下一页使用.
passport.use('local-signup', new LocalStrategy({
usernameField : 'email',
passwordField : 'password',
passReqToCallback : true
},
function(req, email, password, done) {
process.nextTick(function() {
new User({
localEmail: email,
localPassword: User.generateHash(password)
}).save().then(function(model) {
return done(null, model);
});
});
}));
它到达了应该序列化它的地方,但我 运行 遇到错误 Error: failed to serialize user into session
。现在这没有多大意义,因为理论上,我应该通过 return done(null, model);
passport.serializeUser(function(user, done) {
done(null, user.localEmail);
});
passport.deserializeUser(function(email, done) {
new User({localEmail: email}).fetch().then(function(user) {
done(null, user);
});
});
我是不是遗漏了什么明显的东西?任何帮助将不胜感激,谢谢!
Bookshelf 的模型属性不会作为简单的对象属性公开,它们需要一个 get()
方法才能达到(等待我们在那里拥有适当访问器的日子...)
那么,请尝试将您的 serializeUser()
更改为:
passport.serializeUser(function(user, done) {
done(null, user.get('localEmail'));
});