[英]How do I render a pug template on success and failure of authentication(passport.js) with variables from the pug template? (express, passport.js, pug)
我正在使用passport.js 开发登录系统,并使用pug 模板进行表达。 我正在尝试使用失败消息呈现登录页面,如果成功,我想将用户重定向回他们去过的最后一页,其中的 URL 作为隐藏输入传递到 pug 模板中。 然后我想获取该隐藏输入的值并将其放在
successRedirect<\/code>中,但是它说
req is not defined<\/code> :
router.post("/login", body('backurl').trim().escape(), passport.authenticate("local", {
successRedirect: (req.body.backurl),
failWithError: true
}), function(err, req, res, next) {
return res.render('login', { message: 'Unable to login, the password or the username are wrong' })
}
您可以使用 get() 请求呈现视图。 使用 post() 请求,您应该重定向,如果您想向用户显示消息,您可以使用 express-flash 包。
router.post("/login", (req, res, next) => {
passport.authenticate("local", (err, user) => {
if (!user) {
req.flash('error', { msg: 'Unable to login, the password or the username are wrong')
return res.redirect('/login');
}
res.redirect(req.body.backurl);
}
});
通过这种方式,您可以获得有关附加内容的更多详细信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.