gpt4 book ai didi

javascript - 如何在通过 Passport 登录后检查当前正在使用哪个用户。 js

转载 作者:行者123 更新时间:2023-11-30 14:06:52 26 4
gpt4 key购买 nike

我用 passport.js 写了一个登录系统不同的用户组将被路由到不同的路径。

router.post('/signin', passport.authenticate('local', {
failureRedirect: '/fail',
failureFlash: true
}),
(req, res) => {
//if sucess
if (req.body.group == 'GroupA'){
res.redirect('/groupa');
}
else if (req.body.group == 'GroupB') {
res.redirect('/groupb');
}
else {
res.redirect('/groupc');
}
}

我可以成功转到不同的路线,但我基本上无法更改我的网址上的路径以在登录后输入另一条路线(例如以 GroupA 身份登录,然后输入 http://localhost/groupb,我可以进入该路线)我做的“ensureAuthenticated”功能只能检查用户是否登录但不能检查哪个用户登录,那么如何避免用户转到其他路径的问题?(确保优先验证)

function ensureAuthenticated(req, res, next){
if(req.isAuthenticated()) {
console.log(req.body.username);
return next();
}
else {
console.log('You haven\'t login');
res.redirect('/'); //back to login page
}

最佳答案

If authentication succeeds, the next handler will be invoked and the req.user property will be set to the authenticated user.

PassportJs authenticate

关于javascript - 如何在通过 Passport 登录后检查当前正在使用哪个用户。 js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55209175/

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