我遇到了一个非常特殊的问题。我试图让快递+护照上班。由于绝大多数在线示例都是针对Express 3的,所以这个过程花了我一段时间 - 最终我得到了Google身份验证。我试图将Google帐户绑定到本地帐户,这需要从Mongo DB中搜索帐户。以下是有效的代码部分:
//Serialize function
passport.serializeUser(function(user, done) {
done(null, user);
});
//Deserialize function
passport.deserializeUser(function(obj, done) {
done(null, obj);
});
//Strategy
passport.use(new GoogleStrategy({
returnURL: 'http://localhost:8000/auth/google/return',
realm: 'http://localhost:8000/'
},
function(identifier, profile, done) {
process.nextTick(function() {
profile.identifier = identifier;
return done(null, profile);
});
}
));
以下代码经过验证可行。一切都像魅力一样。 但是,当我修改代码时如下:
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
//note that only change is to find user from database, User is a mongoose instance
//very straightforward, verified user can be found.
User.findById(id, function(err, user) {
done(err, user);
});
});
如果找不到,则策略已更改为创建用户:
process.nextTick(function() {
profile.id = identifier;
User.findOne({
'google.id': profile.id
}, function(err, user) {
if (err) {
return done(err); // don't even know what done means!
} else if (user) {
return done(null, user);
} else {
//very easy to create and save to database.
//create a new user instance
var newUser = new User();
newUser.google.id = profile.id;
newUser.google.name = profile.displayName;
newUser.google.email = profile.emails[0].value;
newUser.local.email = newUser.google.email;
newUser.local.name = newUser.google.name;
// newUser.id = newUser._id;
//it should have been saved to database?
newUser.save(function(err) {
if (err)
throw err;
//The done is called once is saved.
return done(null, newUser);
});
}
});
});
这是路由器,它是在护照的所有必要初始化之后放置的。
app.get('/', function(req, res) {
console.log("Rendering...");
console.log(req.user);
res.render('index', {
user: req.user
});
console.log("Rendering done...");
});
现在这里有趣的部分:
当我访问URL
时,app.get('/')
中的console.log在页面加载后被调用,并且req.user
未定义。当它工作时,以正确的顺序调用console.log。似乎在呈现页面时,req.user
尚未初始化 - 但我无法弄清楚为什么在res.render之后调用console.log函数 - 它没有做任何事情对我有意义。这可能是某些异步逻辑的结果......
任何人都有任何想法?谢谢。我会将代码更新为Github
。
我还尝试websocket
访问req.handshake.use
r - 所有数据都在那里。它只是在调用res.render
的时候,它还没有被初始化。