[英]Using redis with Passportjs for sessions does not work
我在ExpressJS上使用Nodejs。 我的應用程序部署在heroku上,我為我的應用程序安裝了“redistogo”插件。
我正在利用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?
您應該使用cookie的選項將其設置為false或ture,但建議為true
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')
})
}));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.