Passport.authenticate(' local',{ResconseRturnToorreDirect:'/'})永远不会返回到以前的路线。仅重定向

发布于 2025-02-13 20:20:06 字数 628 浏览 0 评论 0原文

const router = require('express').Router()
const User = require('../models/user.model')
const { body, validationResult } = require('express-validator')
const passport = require('passport')



router.get('/login', ensureNotAuthenticated, async(req, res, next) => {
res.render('login')
})
router.post('/login', ensureNotAuthenticated,
passport.authenticate('local', { 
successReturnToOrRedirect: '/',
failureRedirect: "/auth/login",
failureFlash: true
}))

例如,如果用户试图访问受保护的路线,例如“ auth/profile”,则他会重定向到登录页面,但是在成功登录时,他会被重定向到“ RessconturntoRettoReDirect”中指定的路线。我使用的是护照0.4.1,在那里,它将用户成功登录后重定向回到先前的受保护路线

const router = require('express').Router()
const User = require('../models/user.model')
const { body, validationResult } = require('express-validator')
const passport = require('passport')



router.get('/login', ensureNotAuthenticated, async(req, res, next) => {
res.render('login')
})
router.post('/login', ensureNotAuthenticated,
passport.authenticate('local', { 
successReturnToOrRedirect: '/',
failureRedirect: "/auth/login",
failureFlash: true
}))

if for example a user tries to access a protected route, say 'auth/profile', he gets redirected to the log in page, but on successfully logging in, he gets redirected to the route specified in 'successReturnToOrRedirect'. I was using passport 0.4.1 and there, it redirected back to the previous protected route after the user has successfully logged in

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只有影子陪我不离不弃 2025-02-20 20:20:06

同样的问题,而Passportjs则从v0.4.1升级到v0.6.0。有一个解决方法可以在您的身份验证功能中添加weepSessionInfo:true

passport.authenticate('local', {
    successReturnToOrRedirect: '/',
    failureRedirect: '/login?attempt=true',
    keepSessionInfo: true,
})

您还可以在

Same problem here while upgrade passportjs from v0.4.1 to v0.6.0. There is a workaround solution to add a keepSessionInfo: true to your authentication function.

passport.authenticate('local', {
    successReturnToOrRedirect: '/',
    failureRedirect: '/login?attempt=true',
    keepSessionInfo: true,
})

You may also find discussions in https://github.com/jaredhanson/passport/issues/919

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文