title: How to automatically do a rest call on jwt token expiry in node js
body: <p>Im using JWT token in my node js application for authentication and when a new token is generated Iam udpating in DB with fields custid,token,sessionValid(boolean).</p>
  
  <p>Now I've set the token expiry to 5 minutes.So once the token is expired it should automatically update the db record and make sessionValid to false.Here I have a rest call to update the DB.The reason for going this approach is Im implementing SSO and the other application would use this same rest call for session validation.</p>
  
  <p>Below is the code how Im generating token</p>
  
  <pre><code>var isExpired = false;
  router.post('/auth', passport.authenticate(  
  'local', {
     session: false
   })generateToken, inserttoken,respond);
  
   function generateToken(req, res, next) {  
      token = jwt.sign({
      id: req.user.id,
      },SECRET, {
      expiresIn: 5 * 60
    });
      req.token = token;
      next();
    }
  
    function inserttoken(req,res,next){
      //In this function Im doing rest call to update the db with the token generated 
    }
  
     function respond(req, res) { 
      res.cookie('customerid', req.user.id, { expires: new Date(new Date().getTime()+1*60*
      1000),httpOnly: true })
      res.cookie('token', req.token, { expires: new Date(new Date().getTime()+1*60*
      1000), httpOnly: true })
      res.status(200).json({
      user: req.user,
      token: req.token
    });
   }
  </code></pre>
  
  <p>So how can I automatically automatically do my rest call and update db when token is expired. Iam even saving these in my cookies as well.</p>
  
  <p>Any help appreciated.Thanks!</p>