我有一個NodeJS和Redis與護照臉書集成的問題。這是我的代碼:NodeJS + Redis + Express +護照Facebook身份驗證失敗
var express = require('express');
var engine = require('ejs').__express;
var app = express();
var mongo = require('mongodb');
var mongoose = require('mongoose');
var hanzi = require('./modules/hanzi');
var session = require('express-session');
var passport = require('passport')
, FacebookStrategy = require('passport-facebook').Strategy
, RedisStore = require('connect-redis')(session)
, redis = require('redis')
, dbRedis = redis.createClient();
Server = mongo.Server,
Db = mongo.Db,
BSON = mongo.BSONPure;
app.configure(function() {
var sessionStore = new RedisStore({
client: dbRedis,
});
app.set('port', process.env.PORT || 8088);
app.set('views', __dirname+'/views');
app.engine('ejs', engine);
app.use(express.favicon(__dirname + '/public/img/favicon.ico'));
app.use(express.cookieParser());
app.use(express.session({
secret: 'im your besta' ,
cookie: {secure: true, maxAge:86400000},
store: sessionStore
}));
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use('/public', express.static(__dirname + '/public'));
app.use(passport.initialize());
app.use(passport.session());
});
但是,當我登錄由於Facebook的出現,我找到合適的用戶,請使用done(null,oldUser);
但我ensureAuthenticated()函數始終是假的......
你有什麼想法?
在此先感謝!
編輯: 序列化/反序列化
passport.serializeUser(function(user, done) {
console.log("serialize user");
console.log(user._id);
done(null, user._id);
});
passport.deserializeUser(function(obj, done) {
console.log("deserialize user");
done(null, obj);
});
passport.use(new FacebookStrategy({
clientID: "xxx",
clientSecret: "xxxx",
callbackURL: "http://myurl/auth/facebook/callback"
},
function(accessToken, refreshToken, profile, done) {
db.collection('fbs', function(err, collection) {
collection.findOne({fbId : profile.id}, function(err, oldUser) {
if(oldUser){
console.log("OK");
console.log(oldUser);
done(null,oldUser);
}else{
console.log("NEW USER");
var newUser = new FbUsers({
fbId : profile.id ,
email : profile.emails[0].value,
name : profile.displayName
}).save(function(err,newUser){
if(err) throw err;
done(null, newUser);
});
}
});
});
}
));
function ensureAuthenticated(req, res, next) {
if (req.isAuthenticated()) { return next(); }
res.redirect('/')
}
編輯2:添加Redis的日誌
+1395994580.698685 (db 2) "setex" "sess:Od0RD3AcyIhTMUynnlDQobEr" "86400" "{\"cookie\":{\"originalMaxAge\":86400000,\"expires\":\"2014-03-29T08:16:20.698Z\",\"secure\":true,\"httpOnly\":true,\"path\":\"/\"},\"passport\":{}}"
+1395994582.099713 (db 2) "setex" "sess:OT1Rht88PPbeMDnKvYjMmGlC" "86400" "{\"cookie\":{\"originalMaxAge\":86400000,\"expires\":\"2014-03-29T08:16:22.099Z\",\"secure\":true,\"httpOnly\":true,\"path\":\"/\"}}"
+1395994582.102495 (db 2) "setex" "sess:nu6Pm2xb4SFpf8IPNFz7oBUa" "86400" "{\"cookie\":{\"originalMaxAge\":86400000,\"expires\":\"2014-03-29T08:16:22.102Z\",\"secure\":true,\"httpOnly\":true,\"path\":\"/\"}}"
+1395994582.104810 (db 2) "setex" "sess:qckQqsbZsEWCrpA1dJEvRdsa" "86400" "{\"cookie\":{\"originalMaxAge\":86400000,\"expires\":\"2014-03-29T08:16:22.104Z\",\"secure\":true,\"httpOnly\":true,\"path\":\"/\"}}"
+1395994582.109756 (db 2) "setex" "sess:WmXRkw2GKWx8DRz25RIwMOWJ" "86400" "{\"cookie\":{\"originalMaxAge\":86400000,\"expires\":\"2014-03-29T08:16:22.109Z\",\"secure\":true,\"httpOnly\":true,\"path\":\"/\"}}"
+1395994582.112338 (db 2) "setex" "sess:3WkCT4YqL1mpeyHXiBwjpWpR" "86400" "{\"cookie\":{\"originalMaxAge\":86400000,\"expires\":\"2014-03-29T08:16:22.112Z\",\"secure\":true,\"httpOnly\":true,\"path\":\"/\"}}"
+1395994582.117175 (db 2) "setex" "sess:yKaQ2Z5QJaC8UG1dw2q0sZMo" "86400" "{\"cookie\":{\"originalMaxAge\":86400000,\"expires\":\"2014-03-29T08:16:22.117Z\",\"secure\":true,\"httpOnly\":true,\"path\":\"/\"}}"
+1395994582.248297 (db 2) "setex" "sess:m2xlPaDlFCnVelxFnvmWEnKJ" "86400" "{\"cookie\":{\"originalMaxAge\":86400000,\"expires\":\"2014-03-29T08:16:22.248Z\",\"secure\":true,\"httpOnly\":true,\"path\":\"/\"}}"
+1395994582.964619 (db 2) "setex" "sess:XWQeeUCS7JUZTPzxZSMxqUpR" "86400" "{\"cookie\":{\"originalMaxAge\":86400000,\"expires\":\"2014-03-29T08:16:22.964Z\",\"secure\":true,\"httpOnly\":true,\"path\":\"/\"}}"
+1395994582.970229 (db 2) "setex" "sess:LqgRcHrnIaXfXVceHgdZ6FlN" "86399" "{\"cookie\":{\"originalMaxAge\":86399999,\"expires\":\"2014-03-29T08:16:22.969Z\",\"secure\":true,\"httpOnly\":true,\"path\":\"/\"}}"
+1395994588.902167 (db 2) "setex" "sess:qXO9gBrHu8vp64UyKYJHWcCq" "86400" "{\"cookie\":{\"originalMaxAge\":86400000,\"expires\":\"2014-03-29T08:16:28.900Z\",\"secure\":true,\"httpOnly\":true,\"path\":\"/\"},\"passport\":{}}"
+1395994590.304890 (db 2) "setex" "sess:JA4KfLN92wVIYrcOyhZeQw2O" "86400" "{\"cookie\":{\"originalMaxAge\":86400000,\"expires\":\"2014-03-29T08:16:30.304Z\",\"secure\":true,\"httpOnly\":true,\"path\":\"/\"},\"passport\":{\"user\":\"52de69b0c432647c5fc6309c\"}}"
+1395994590.690489 (db 2) "setex" "sess:Vc6Z6oLnnxGqqzItuEWpO8ab" "86400" "{\"cookie\":{\"originalMaxAge\":86400000,\"expires\":\"2014-03-29T08:16:30.690Z\",\"secure\":true,\"httpOnly\":true,\"path\":\"/\"},\"passport\":{}}"
+1395994591.582708 (db 2) "setex" "sess:XIud7BMHoPjJ0BFf6AMkrJV9" "86400" "{\"cookie\":{\"originalMaxAge\":86400000,\"expires\":\"2014-03-29T08:16:31.582Z\",\"secure\":true,\"httpOnly\":true,\"path\":\"/\"}}"
+1395994591.585150 (db 2) "setex" "sess:IlHl8a7QDp2g9UpXAmYHOk09" "86400" "{\"cookie\":{\"originalMaxAge\":86400000,\"expires\":\"2014-03-29T08:16:31.585Z\",\"secure\":true,\"httpOnly\":true,\"path\":\"/\"}}"
+1395994591.631536 (db 2) "setex" "sess:jeLL5UZkPXEeK8K3mbCaBwrl" "86400" "{\"cookie\":{\"originalMaxAge\":86400000,\"expires\":\"2014-03-29T08:16:31.631Z\",\"secure\":true,\"httpOnly\":true,\"path\":\"/\"}}"
+1395994591.634714 (db 2) "setex" "sess:xIgC6NKShHW8ewG8YGSCm0vn" "86400" "{\"cookie\":{\"originalMaxAge\":86400000,\"expires\":\"2014-03-29T08:16:31.634Z\",\"secure\":true,\"httpOnly\":true,\"path\":\"/\"}}"
+1395994591.638343 (db 2) "setex" "sess:SxfiYe5TBeUGrNQ4z3MPIBHe" "86400" "{\"cookie\":{\"originalMaxAge\":86400000,\"expires\":\"2014-03-29T08:16:31.638Z\",\"secure\":true,\"httpOnly\":true,\"path\":\"/\"}}"
+1395994591.953461 (db 2) "setex" "sess:JK0nt4aD0ycED0kjiAEPZEPD" "86400" "{\"cookie\":{\"originalMaxAge\":86400000,\"expires\":\"2014-03-29T08:16:31.953Z\",\"secure\":true,\"httpOnly\":true,\"path\":\"/\"}}"
+1395994591.987201 (db 2) "setex" "sess:B6TOzQWvBo8HiN55so28iL8X" "86400" "{\"cookie\":{\"originalMaxAge\":86400000,\"expires\":\"2014-03-29T08:16:31.987Z\",\"secure\":true,\"httpOnly\":true,\"path\":\"/\"}}"
+1395994592.033600 (db 2) "setex" "sess:BoQODHZ8kjgqczNAe7By8kEs" "86400" "{\"cookie\":{\"originalMaxAge\":86400000,\"expires\":\"2014-03-29T08:16:32.033Z\",\"secure\":true,\"httpOnly\":true,\"path\":\"/\"}}"
+1395994592.036671 (db 2) "setex" "sess:lmeCXBZUr0dw38iozFNOBayK" "86400" "{\"cookie\":{\"originalMaxAge\":86400000,\"expires\":\"2014-03-29T08:16:32.036Z\",\"secure\":true,\"httpOnly\":true,\"path\":\"/\"}}"
我不知道爲什麼我加這麼多餅乾...任何想法?
能否請你添加護照配置代碼和ensureAuthenticated()函數 –
完成!謝謝 – Immikims
在'passport.deserializeUser()'中,你必須通過用戶id(即'obj')找到用戶對象user,並調用'done(null,user);'。 – bnuhero