2013-04-24 179 views
2

我一直在掙扎幾天,我取得了一些很好的進展,但是我無法讓我的會話工作得很好。爲什麼我的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 
+1

這聽起來像你可能對你的會話cookie不正確的到期,但請出示代碼(ESP您的應用程序和Passport配置),否則我只是在猜測。沒有代碼的 – robertklep 2013-04-24 06:53:53

+0

不可能告訴你什麼是錯的。你檢查了[示例](https://github.com/jaredhanson/passport-facebook/tree/master/examples/login)附帶護照-Facebook – balazs 2013-04-24 07:16:07

+0

你寫*「如果我點擊登錄到FB按鈕它會被稱爲「*關於'deserializeUser'。你打電話給那個按鈕時會調用哪條路線? 'deserializeUser'從來就不是爲'/'路由調用的,或者它不起作用?由於你沒有處理'FacebookStrategy'回調中的錯誤,這可能是一個問題嗎? – robertklep 2013-04-24 15:09:48

回答

2

的問題是在你的serializeUserdeserializeUser功能。

正如你注意到沒有,當你點擊FBlogin按鈕deserializeUser被稱爲第一和唯一的一次 - 其實這就是所謂的與已通過serializeUser功能以前只返回前id。如果此時無法通過id找到用戶,則passport.user不會保存到會話中。

因此,在以下請求中passport.deserializeUser根本不會被調用,因爲express.session不會使用護照的用戶標識填充req.session

綜上所述:你需要檢查,如果你的serializeUser返回一個id相比,可以理解和您deserializeUser反序列化。

FYI:正確req.user對象的身份驗證的用戶應該是這樣的:

{ 
    cookie: { path: '/', _expires: null, originalMaxAge: null, httpOnly: true }, 
    passport: { user: 51772291b3367f0000000001 } 
} 
+0

所以我把一個console.log看看我的序列化方法是否被調用......它不是,除非我點擊我的FB按鈕。當我點擊FB按鈕時,它會被調用,然後我的反序列化被調用,並且一切正常。另一個實驗告訴我,如果我關閉瀏覽器並返回,會話仍然有效。如果我重啓我的服務器,它就消失了。這讓我覺得我的cookie基於會話。我如何更新cookie護照製作,使其持續時間超過一個會話? – David 2013-04-24 22:39:14

+0

供參考:這是我的cookie的輸出。 Cookie:{「cookie」:{「originalMaxAge」:35999999,「expires」:「2013-04-25T08:48:38.994Z」,「httpOnly」:true,「路徑」:「/」},「護照」: { 「用戶」: 「775119337」}}。我在這篇文章的基礎上添加了MaxAge,但它沒有幫助。 http://stackoverflow.com/questions/15016551/node-js-express-passport-cookie-expiration – David 2013-04-24 22:52:13

+1

@David因爲你沒有使用會話存儲,Express將默認使用'MemoryStore'來存儲會話,這意味着一旦你的服務器重新啓動,會話就消失了。您應該使用持久性存儲(如[connect-mongo](https://github.com/kcbanner/connect-mongo)),或者使用[基於cookie的會話](http://expressjs.com/ api.html#cookieSession)。 – robertklep 2013-04-25 06:35:52