我一直在掙扎幾天,我取得了一些很好的進展,但是我無法讓我的會話工作得很好。爲什麼我的req.user在加載時始終爲空?
我已經成功地使用Passport來與Facebook進行身份驗證。當我用FB按鈕點擊我的登錄時,會話完全加載,我的req.user對象都準備好了,但在我看來,我應該只需要做一次。
我知道護照保存了一個cookie。我已經查閱並看到了工作。
我想檢測用戶是否已登錄我的索引頁的負載,但是當我加載頁面時,req.user對象始終爲空,我的passport.deserializeUser方法永遠不會調用來加載它(如果我點擊進入FB按鈕的日誌,它確實會被調用)。
所以我的問題是,你如何告訴護照頁面加載檢查cookie並加載用戶會話,如果有的話?
更新 - 好了,對於那些在日後發現這個問題,我只想去了什麼,我在這裏學到(感謝你們誰評論)。希望它能幫助其他人。
我習慣了無論服務器在哪裏都能存在的.NET cookies。 Node.js和護照不在同一個前提下工作。默認情況下,node.js使用內存存儲來保持其會話。當您關閉終端/命令行中的節點(每次更改服務器代碼時必須執行此操作),存儲器Cookie cookie信息將被重置,因此與其關聯的任何cookie都沒有意義。
爲了解決這個問題,我安裝了Redis(http://cook.coredump.me/post/18886668039/brew-install-redis)並將它掛上了我的店鋪(http://www.hacksparrow.com/use-redisstore-instead-of-memorystore-express-js-in-production.html)。
這將在Azure上工作,這是我計劃的生產服務器,因此一切正常。
好的,這裏有一些代碼。我不知道哪些部分要忍受...
這是server.js
/**
* Module dependencies.
*/
var express = require('express')
, app = express()
, partials = require('express-partials')
, http = require('http')
, server = http.createServer(app)
, io = require('socket.io').listen(server)
, routes = require('./routes')
// facebook
, passport = require('passport')
, facebookStrategy = require('passport-facebook').Strategy
// custom stuff
, urlCommand = require('./middleware/UrlCommand')
, azureCommand = require('./middleware/AzureCommand')
, userCommand = require('./middleware/UserCommand');
// Ports
var port = process.env.port;
if (isNaN(port))
port = 3000;
server.listen(port);
//allows the use of Layouts
app.use(partials());
passport.serializeUser(function(user, done) {
done(null, user.RowKey);
});
passport.deserializeUser(function (id, done) {
console.log("deserialize");
userCommand.findByID(id, function (err, user) {
done(err, user);
});
});
// Configuration
app.configure(function() {
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.cookieParser());
app.use(express.bodyParser());
app.use(express.session({ secret: 'SECRET!' }));
app.use(express.methodOverride());
app.use(passport.initialize());
app.use(passport.session());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(express.errorHandler());
});
// Facebook
passport.use(new facebookStrategy({
clientID: CLIENT,
clientSecret: "SECRET",
callbackURL: "http://localhost:3000/auth/facebook/callback" //DEV
},
function (accessToken, refreshToken, profile, done) {
userCommand.findOrCreate(profile.id, profile.name.givenName, profile.name.familyName, profile.emails[0].value, accessToken, function (error, user) {
return done(null, user);
});
}
));
// Routes
app.get('/', routes.index);
app.get('/auth/facebook', passport.authenticate('facebook', { scope: 'email' }));
app.get('/auth/facebook/callback', passport.authenticate('facebook', { successRedirect: '/',
failureRedirect: '/login' }));
// Sockets
io.sockets.on('connection', function (socket) {
// when the client emits 'sendURL', this listens and executes
socket.on('sendURL', function (data) {
console.log('sendURL called: ' + data);
urlCommand.AddURL(data);
// we tell the client to execute 'processURL'
io.sockets.emit('urlComplete', data);
});
});
console.log("Express server listening on port %d in %s mode", port, app.settings.env);
index.js
exports.index = function (req, res) {
console.log(req.user); // ALWAYS NULL
res.render('index', { title: 'Express' })
};
UserCommand
var azureCommand = require('../middleware/AzureCommand');
var tableService = azureCommand.CreateTableService();
function findByID(id, callback) {
console.log('FindByID');
tableService.queryEntity('user', 'user', id, function (error, entity) {
console.log('Found him: ' + entity.Email);
callback(error, entity);
});
}
function findOrCreate(id, first, last, email, accessToken, callback) {
var user = {
PartitionKey: 'user'
, RowKey: id
, First: first
, Last: last
, Email: email
, AccessToken: accessToken
}
tableService.insertEntity('user', user, function (error) {
callback(null, user);
});
}
exports.findByID = findByID;
exports.findOrCreate = findOrCreate;
這是我的輸出日誌顯示,當我輸出我的會議...
node server.js
info - socket.io started
Express server listening on port 3000 in development mode
{ cookie:
{ path: '/',
_expires: null,
originalMaxAge: null,
httpOnly: true },
passport: {}
}
debug - served static content /socket.io.js
這聽起來像你可能對你的會話cookie不正確的到期,但請出示代碼(ESP您的應用程序和Passport配置),否則我只是在猜測。沒有代碼的 – robertklep 2013-04-24 06:53:53
不可能告訴你什麼是錯的。你檢查了[示例](https://github.com/jaredhanson/passport-facebook/tree/master/examples/login)附帶護照-Facebook – balazs 2013-04-24 07:16:07
你寫*「如果我點擊登錄到FB按鈕它會被稱爲「*關於'deserializeUser'。你打電話給那個按鈕時會調用哪條路線? 'deserializeUser'從來就不是爲'/'路由調用的,或者它不起作用?由於你沒有處理'FacebookStrategy'回調中的錯誤,這可能是一個問題嗎? – robertklep 2013-04-24 15:09:48