【问题标题】:Running an passed function and passport.authenticate运行传递的函数和 passport.authenticate
【发布时间】:2017-06-04 10:38:53
【问题描述】:

我希望设置 JWT 和 passport.authenticate 的函数都运行,但只有前者在运行。

有什么方法可以同时运行吗?

router.post('/login', (req, res, next) => {

   const userEmail = req.body.username;

   User.getUserByEmail(userEmail, function(err, user) {

     const token = jwt.sign(user, config.secret, {
       expiresIn: 604800 // 1 week
     });
     new Cookies(req, res).set('access_tokenx', token, {
       httpOnly: true,
       secure: false
     });
     return res.send();
   });
 },
 passport.authenticate('local', {
   successRedirect: '/',
   failureRedirect: '/users/login',
   failureFlash: true
 }),
 function(req, res) {
   res.redirect('/');
 });

【问题讨论】:

    标签: node.js jwt backend passport.js


    【解决方案1】:

    来自expressdocs

    如果当前中间件函数没有结束请求-响应 循环,它必须调用 next() 将控制权传递给下一个中间件 功能。

    由于您尝试运行两个中间件函数和一个“最终”请求处理器,因此您应该在设置 JWT 的函数中将 return res.send(); 替换为 next()

    另外请记住,如果出现任何错误,您应该致电next(err)。 (你不应该允许请求在中间件函数中未经处理,因为客户端永远不会收到响应)。

    【讨论】:

    • 它工作得很好,非常感谢,这已经造成了多年的麻烦。祝您一切顺利,度过美好的一天、一周、一个月、一年和一生!
    猜你喜欢
    • 2017-10-29
    • 1970-01-01
    • 1970-01-01
    • 2017-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-26
    相关资源
    最近更新 更多