title: JWT expired, unexpected response in nodejs API
body: <p>i have a problem when i try to use a private api in my node.js server, This is the process that i am following</p>
  <ol>
  <li>SignUp (if the user doesn't have an account) or logIn (already have an account).</li>
  <li>It generates the token and i pass it in the header <code>res.header('access_token': token)</code></li>
  <li>I copy this token and paste it in my private api in the header section (i'm using postman to test it for now) in order to verify if the user is logged in.</li>
  <li>In my route, i use a middleware to turn it private, validating if the user can use the resource <code>jwt.verify(authorization[1], process.env.SEED_AUTH)</code> (SEED_AUTH is my token secret stored in my server)</li>
  </ol>
  <p>Here is when i have the error, the middleware is failling to verify the user and throw me this error <code>jwt expired</code></p>
  <p><strong>This is my route</strong></p>
  <pre><code>const UsersController = require('../controllers/users.controller')
  const AuthorizationMiddleware = require('../middlewares/authorization.middleware')
  
  exports.routesConfig = (app) =&gt; {
  
    app.post('/api/users',[
      AuthorizationMiddleware.verifyValidJWT
    ], UsersController.insert)
  }
  </code></pre>
  <p><strong>This is my middleware</strong></p>
  <pre><code>const jwt = require('jsonwebtoken')
  require('../config/env.config')
  
  exports.verifyValidJWT = (req, res, next) =&gt; {
    if (req.headers['access-token']) {
      try {
        let authorization = req.headers['access-token'].split(' ');
        if (authorization[0] !== 'Bearer') {
          return res.status(401).json({
            ok: false,
            err: &quot;Unauthorized, Need a valid token&quot;
          });
        } else {
          console.log(authorization[1]);
          req.jwt = jwt.verify(authorization[1], process.env.SEED_AUTH);
          return next();
        }
      } catch (error) {
        return res.status(403).json({
          ok: false,
          err: &quot;Forbidden, need a valid token -&gt; &quot; + error.message
        });
      }
    } else {
      return res.status(401).json({
        ok: false,
        err: &quot;Need to recieve a valid token&quot;
      });
    }
  }
  </code></pre>
  <p><strong>And finally the API  UsersController.insert</strong></p>
  <p>What i'm trying to do with this api is to create a new user.</p>
  <p><strong>For a better understanding this is my LOGIN API</strong></p>
  <pre><code>const User = require('../models/users.model')
  const { signUpValidation, logInValidation } = require('../middlewares/auth.validation.data')
  const bcrypt = require('bcrypt')
  const jwt = require('jsonwebtoken')
  
  exports.logIn = async (req, res) =&gt; {
  
    let body = req.body;
  
    //Validation
    const { error } = logInValidation(body)
   
    if (error) {
      return res.status(400).json({
        ok: false,
        err: error.details[0].message
      })
    }
  
    //Check if the email already exists in the database
    const user = await User.findOne({ email: body.email })
    if (!user) {
      return res.status(400).json({
        ok: false,
        err: &quot;Invalid Email or password!&quot;
      })
    }
  
    const validPass = await bcrypt.compareSync(body.password, user.password)
    if (!validPass) {
      return res.status(400).json({
        ok: false,
        err: &quot;Invalid Email or password!&quot;
      })
    }
    
    const token = jwt.sign(
      {
        _id: user._id,
        email: user.email
      },
      process.env.SEED_AUTH,
      {
        expiresIn: process.env.TOKEN_EXPIRY
      }
    )
    
    res.header('access-token', token).json({
      ok: true,
      user: {
        id: user._id,
        email: user.email
      }
    });
  } 
  </code></pre>
  <p><strong>SignUp and LogIn validation</strong>
  I use these middleware to verify if it is a valid email, and the name with a minimum number of letters...</p>
  <p>My <code>process.env.TOKEN_EXPIRY</code> is set to <code>300</code> (i understand that, it is in seconds), i've tried with bigger number though</p>
  <p>(The API works without the middleware).</p>
  <p>What would be the problem that i am not seeing. Thanks for your help.</p>