我正在使用passport-facebook来处理我网站的 facebook 登录。以下是相关代码:
不太重要:
passport.use(new FacebookStrategy({
// pull in our app id and secret from our auth.js file
clientID: configAuth.facebookAuth.clientID,
clientSecret: configAuth.facebookAuth.clientSecret,
callbackURL: configAuth.facebookAuth.callbackURL
},
// facebook will send back the token and profile
function (token, refreshToken, profile, done) {
// asynchronous
process.nextTick(function () {
//check for the user in db
...
if(user found..){
return done(null, user); // user found, return that user
}
else{
// if there is no user found with that email id, create them
var newUser = {};
............
return done(null, newUser);
}
序列化反序列化:
// used to serialize the user for the session
passport.serializeUser(function (user, done) {
console.log('serializing user.');
done(null, user.id);
});
// used to deserialize the user
passport.deserializeUser(function (id, done) {
getDBHandle(function (db) {
db.collection('users').findOne({'id':id}, function (err, user) {
console.log('deserialize user.',id,user);
done(err, user);
});
});
});
Caller: // facebook 认证和登录的路由
app.get('/auth/facebook', passport.authenticate('facebook', { scope : 'email' }));
// handle the callback after facebook has authenticated the user
app.get('/auth/facebook/callback',
passport.authenticate('facebook', {
successRedirect : '/profile',
failureRedirect : '/'
}));
重要:一旦用户登录,登录的用户信息将req.user
按照护照保存。
app.get('/isAuthenticated',function(req,res){
...
res.send(req.user);
});
现在让我们说,用户从她的 Facebook 帐户注销,护照如何知道发生了注销事件。
本质上,如果用户从 fb 注销,那么即使我们的应用程序也应该认为该用户未登录。
isAuthenticated
我在注销我的 fb 后尝试打电话,护照仍然返回相同的用户信息。
我应该如何处理这种情况?
我是否需要/auth/facebook
每次都打电话来检查用户是否经过身份验证,而不是依赖于req.user
?