繁体   English   中英

"<i>How do I render a pug template on success and failure of authentication(passport.js) with variables from the pug template?<\/i>如何使用哈巴狗模板中的变量在身份验证(passport.js)成功和失败时呈现哈巴狗模板?<\/b> <i>(express, passport.js, pug)<\/i> (快递,passport.js,哈巴狗)<\/b>"

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM