title: Node.js JWT refresh token in express middleware
body: <p>I'm trying to configure a token refresh method in my express middleware in wich the token is validate at every request to the api. I will check if the token expired and if so, I will sign a new token with new exp date. The problem is that I have to send the token again, but doing thatI lose the original request to send the token with the response and the api not continue to the destination endpoint.
  How I can send back the new refreshed token and continue with the request?</p>
  
  <p>My express middleware to check the token:</p>
  
  <pre><code>apiRouter.use(function(req, res, next) {
        var token = req.body.token || req.query.token || req.headers['x-access-token'];
        if (token) {
          jwt.verify(token, app.get('superSecret'), function(err, decoded) {
            if (err) {
              //Here I can check if the received token in the request expired
              if(err.name == "TokenExpiredError"){
                  var refreshedToken = jwt.sign({
                      success: true,
                      }, app.get('superSecret'), {
                          expiresIn: '5m'
                      });
                  //Here need to send the new token back to the client and continue with the request
                  //but if I use return res.... the request don't continue to next()
                  next();
                }else if (err) {
                  return res.json({ success: false, message: 'Failed to authenticate token.' });
                }         
            } else {
              //If no error with the token, continue 
              next();
            };
          });
        } else {
          return res.status(403).send({
              success: false,
              message: 'No token provided.'
          });
        }
      });
  </code></pre>
  
  <p>I dont' know if its the best aproach to this.</p>
  
  <p>Thanks you.</p>