gpt4 book ai didi

node.js - 带有 Passportjs 的客户端 session 模块

转载 作者:太空宇宙 更新时间:2023-11-04 02:22:49 24 4
gpt4 key购买 nike

我的代码粘贴在下面。在回调方法中,我设置了用户,但是当我重定向到“/”时,该用户不再可用。我正在使用护照和客户 session 。任何帮助将不胜感激。我最初使用 req.session 并按照此 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 中,我有以下代码:

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());

最佳答案

我找到了this article非常有用,尤其是这个特定的部分:

app.use(function(req, res, next) {
if (req.session && req.session.user) {
User.findOne({ email: req.session.user.email }, function(err, user) {
if (user) {
req.user = user;
delete req.user.password; // delete the password from the session
req.session.user = user; //refresh the session value
res.locals.user = user;
}
// finishing processing the middleware and run the route
next();
});
} else {
next();
}
});

关于node.js - 带有 Passportjs 的客户端 session 模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32250185/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com