Nodejs – Passport – 无法序列化用户进入会话
我正在使用下面的库:
护照的oauth2密码发放
我想要做的是以下几点:
- validation到API(用户名/密码)
- 获取访问令牌
- 从API获取用户configuration文件
- logging用户使用护照
我正在使用下面的实现:
PasswordGrantStrategy.prototype.userProfile = function(accessToken, done) { return done(null, { real_name: 'Foo Baz', email: 'test@test.com' }); } passport.use(new PasswordGrantStrategy({ tokenURL: 'http://api.local/oauth/token', clientID: "2", clientSecret: "", grantType: "password", }, function(accessToken, refreshToken, profile, done) { return done(null, profile); })); function authenticate() { return function(req, res, next) { var username = "email@email.com"; var password = "password"; passport.authenticate('password-grant', { username: username, password: password })(req, res, next); }; }
使用用户名/密码login到API
它传递到passport.use(new PasswordGrantStrategy
的callback函数的configuration文件。 passport.use(new PasswordGrantStrategy
但是,我收到以下错误:
无法将用户序列化到会话中
它应该是序列化,因为我通过一个configuration文件,一切看起来很好。 现在这是否意味着会话可能会有问题?
您需要为Passport提供serializeUser和deserializeUser方法才能正常工作。
从官方文档:
passport.serializeUser(function(user, done) { done(null, user.id); }); passport.deserializeUser(function(id, done) { User.findById(id, function(err, user) { done(err, user); }); });