title: How can I get a value from the JWT header into a variable in nodejs?
body: <p>I am making an API server where I can login and see some information. Whenever I log in I get a JWT token which contains my username. I want to store this username in a variable whenever I want to use it. This is the code I use to verify my token when I want to enter certain webpages.</p>
  <pre><code>import jwt from &quot;jsonwebtoken&quot;;
  
  function authenticateToken(req, res, next) {
    const authHeader = req.header(&quot;access-token&quot;);
    
    if (!authHeader) return res.sendStatus(401);
  
    jwt.verify(authHeader, process.env.ACCESS_TOKEN_SECRET, (err, user) =&gt; {
      if (err) return res.sendStatus(403);
      req.user = user;
      next();
    });
  }
  
  export default authenticateToken;
  </code></pre>
  <p>It checks if the header contains a JWT token. If so it verifies it. Then at the bottom the user gets retrieved, but I dont know how to get this value.</p>
  <p>On my routes I secure them like this:</p>
  <pre><code>router.get(&quot;/&quot;, authenticateToken, getData);
  </code></pre>
  <p>within the getData method im displaying data from a mysql database. I want to add a check to see if my username is equal to the data in getData (since you can only see certain data).</p>
  <p>In getData i get my data like this:</p>
  <pre><code>connection.query(
    &quot;SELECT * FROM data WHERE userid = ?&quot;, [user.username],
    (err, rows) =&gt;
  </code></pre>
  <p>On the [user.username] spot I want to retrieve the username within the JWT token. I want to do this using a method, but I cant seem to get it. How can I get my username?</p>