title: Node.js callback for jwt.verify()
body: <p>I have an authentication route on my Node.js server that authenticates requests:</p>
  
  <pre><code>app.get('/loggedin', auth, function(req, res){
      console.log(req.authenticated);
      res.send(req.authenticated ? req.authenticated: false)
  })
  </code></pre>
  
  <p>As I understand it, <code>auth</code> is run before <code>app.get()</code>. Here is the code for <code>auth</code>:</p>
  
  <pre><code>var jwt = require('jsonwebtoken');
  var config = require('./config');
  
  module.exports = function(req,res,next){
      var bearerHeader = req.headers['authorization'];
      var token;
      console.log(bearerHeader);
      req.authenticated = false;
      if (bearerHeader){
          console.log("11111");
          var bearer = bearerHeader.split(" ");
          token = bearer[1];
          jwt.verify(token, config.secret, function (err, decoded){
              console.log("22222");
              if (err){
                  console.log(err);
                  req.authenticated = false;
                  req.decoded = null;
              } else {
                  console.log("33333");
                  req.decoded = decoded;
                  req.authenticated = true;
              }
          });
      }
      next();
  }
  </code></pre>
  
  <p>On the server log, however, I receive the following output:</p>
  
  <pre><code>Bearer jsflkdjlsdfjksodfkjlsdfjkls
  11111
  false
  22222
  33333
  </code></pre>
  
  <p>This means that there is a token on the client's side, and that is passes the jwt verification. However, the server decides to begin running <code>app.get()</code> before it finishes returning information in the authentication callback. What gives?</p>