2014-07-03 45 views
3

我正在使用ExpressJS的Nodejs。我的應用程序部署在heroku上,在那裏爲我的應用程序安裝了「redistogo」附加組件。在會話中使用redis和Passportjs不起作用

我正在使用PassportJS來幫助進行身份驗證和會話。

我想在用戶登錄後使用redis存儲會話,以便在應用程序重新啓動時登錄的用戶不會丟失會話。

這是我的代碼(剝離到最低限度爲簡潔起見):

var RedisStore = require('connect-redis')(express); 

app.configure('development', function() { 
    var redisUrl = url.parse(process.env.REDISTOGO_URL), 
     redisAuth = redisUrl.auth.split(':'); 

    app.set('redisHost', redisUrl.hostname); 
    app.set('redisPort', redisUrl.port); 
    app.set('redisDb', redisAuth[0]); 
    app.set('redisPass', redisAuth[1]); 
}); 

app.configure(function() { 
    app.use(express.session({ 
     secret: 'My secret key', 
     store: new RedisStore({ 
      host: app.get('redisHost'), 
      port: app.get('redisPort'), 
      db: app.get('redisDb'), 
      pass: app.get('redisPass') 
     }) 
    })); 
    //Passport configuration 
    app.use(passport.initialize()); 
    app.use(passport.session()); 

}); 

我用請求request.isAuthenticated()以驗證用戶登錄

如果,用戶登錄基礎上的。控制檯語句,我看到登錄是成功的。 PassportJS正確記錄用戶。但是,request.isAuthenticated()返回false。

如果我刪除了redis代碼並僅保留PassportJS代碼,那麼它完美地工作。 我還需要做些什麼才能使用redis和PassortJS?

回答

0

,你應該用你的cookie,要麼設置爲false或自命的選項,但推薦的是真實的

app.use(express.session({ 
    secret: 'My secret key', 

    // add this line 
    cookie : {secure : true}, 


    store: new RedisStore({ 
     host: app.get('redisHost'), 
     port: app.get('redisPort'), 
     db: app.get('redisDb'), 
     pass: app.get('redisPass') 
    }) 
}));