Passport.authenticate(' local',{ResconseRturnToorreDirect:'/'})永远不会返回到以前的路线。仅重定向
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
同样的问题,而Passportjs则从v0.4.1升级到v0.6.0。有一个解决方法可以在您的身份验证功能中添加
weepSessionInfo: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.You may also find discussions in https://github.com/jaredhanson/passport/issues/919