title: How to properly get jwt token from headers and use it with express
body: <p>I created both frontend and backend in express.js with auth &amp; authorization (jwt), now, jwt-token is properly set when I try to login as you can see from this picture below: (the auth-token key)</p>
  <p><a href="https://i.stack.imgur.com/qK45A.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/qK45A.png" alt="headers" /></a></p>
  <p>But how do I get the headers and then use it so the user gets access to his/her data ?</p>
  <p>This is how I have created the token in login process</p>
  <pre class="lang-js prettyprint-override"><code>  const token = jwt.sign({_id: user._id}, &quot;SECRET&quot;);
    
    res.header(&quot;auth-token&quot;, token).send({status: &quot;done&quot;, token: token});
  </code></pre>
  <p>This is a middleware for verifying</p>
  <pre class="lang-js prettyprint-override"><code>module.exports = function (req, res, next) {
    
    //getting the token from headers to check if token is present or not
    const token = req.header(&quot;auth-token&quot;);
    if(!token) return res.status(401).send(&quot;Access Denied&quot;);
    
    try {
      const verified = jwt.verify(token, &quot;SECRET&quot;);
      req.user = verified;
      next();
    } catch(err) {
      res.status(400).send(&quot;Token is Invalid&quot;)
    }
  }
  </code></pre>
  <p>This is a part of my server.js file</p>
  <pre class="lang-js prettyprint-override"><code>app.user(express.json());
  
  app.use(&quot;/user&quot;, verifyJWT);
  
  app.get(&quot;/user&quot;, (req, res) =&gt; {
    res.sendFile(__dirname + &quot;/frontend/userDetails.html&quot;)
  });
  </code></pre>
  <p>Now every time I login and visit the /user route it shows &quot;Access Denied&quot; what am i doing wrong ?</p>