带有passportjs的客户端会话模块 [英] client-sessions module with passportjs

查看:58
本文介绍了带有passportjs的客户端会话模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码粘贴在下面.在回调方法中,我设置了用户,但是当我重定向到/"时,用户不再可用.我正在使用护照和客户端会话.任何帮助将不胜感激.我最初使用 req.session 并按照此链接

My code is pasted below. In the callback method, I set the user, but when I redirect to '/' the user is no longer available. I'm using passport and client-sessions. Any help would be greatly appreciated. I was initially using req.session and changed it as per this link

谢谢.

app.get('/', function (req, res) {
if (req.session_state.user == null) {
passport.authenticate('azureoauth', { failureRedirect: './'})
}
else {
res.render('index', {user: req.session_state.user});
}
});

//This gets called by an external internet application

app.get('/auth/azureOAuth/callback', 
passport.authenticate('azureoauth', {}),
function (req, res) {
req.session_state.user = req.user;
res.redirect("/");
});

在我的 app.js 中,我有以下代码:

And in my app.js, I have the following code:

const clientSession = require("client-sessions");
app.use(clientSession({secret: 'blablabla', duration: 1000 * 60 * 60 * 24 * 365 * 10}));
app.use(passport.initialize()); // for user authentication/authorization
app.use(passport.session());

推荐答案

Passport 期望会话 cookie 被命名为会话".只要您设置 {cookieName: 'session'}client-sessions 就可以作为 express-session 的替代品.确保删除 express-session 否则两者会冲突.

Passport expects the session cookie to be named "session". client-sessions can be used as a drop-in replacement for express-session so long as you set {cookieName: 'session'}. Make sure to remove express-session or the two will conflict.

const config = require('config');
const express = require('express');
const passport = require('passport');
const sessions = require('client-sessions');

const app = express();

// passport expects the cookie to be named "session"
app.use(sessions({
    secret: config.get('SESSION_SECRET'), // extremely secret
    cookieName: 'session', // automatically used by passport sessions
}));

// config passport
passport.use(SomeStrategy);
passport.serializeUser((user, done) => done(null, JSON.stringify(user)));
passport.deserializeUser((userStr, done) => done(null, JSON.parse(userStr)));
app.use(passport.initialize());
app.use(passport.session());

这篇关于带有passportjs的客户端会话模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆