运行 一个传递函数和 passport.authenticate

Running an passed function and passport.authenticate

我想要将 JWT 和 passport.authenticate 设置为 运行 的功能,但只有前者是 运行ning。

有没有办法让我同时拥有两者 运行?

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('/');
 });

来自express docs:

If the current middleware function does not end the request-response cycle, it must call next() to pass control to the next middleware function.

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

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