title: JWT Login - No authorization token was found in middleware
body: <p>I followed a tutorial to add login and registration to my Node.js app using JWT token and I'm having a hard time logging in and redirecting to my 'logged in' admin page. User registration works great, but the login portion I can't figure out.</p>
  
  <p>This is the tutorial I was following:
  <a href="https://medium.freecodecamp.org/learn-how-to-handle-authentication-with-node-using-passport-js-4a56ed18e81e" rel="nofollow noreferrer">https://medium.freecodecamp.org/learn-how-to-handle-authentication-with-node-using-passport-js-4a56ed18e81e</a></p>
  
  <p>My code for login looks like this:</p>
  
  <pre><code>router.post('/login', auth.optional, (req, res, next) =&gt; {
  console.log(req.body);
  
  var user = {
      email: req.body.email,
      password: req.body.password
  }
  
  if (!user.email) {
    return res.status(422).json({
      errors: {
        email: 'is required',
      },
    });
  }
  
  if (!user.password) {
    return res.status(422).json({
      errors: {
        password: 'is required',
      },
    });
  }
  
  return passport.authenticate('local', { session: false }, (err, passportUser, info) =&gt; {
  
      if (err) {
          return next(err);
      }
  
      if (passportUser) {
          const user = passportUser;
          user.token = passportUser.generateJWT();
          console.log("TOKEN: " + user.token);
  
          res.setHeader('Authorization', 'Token ' + user.token);
  
          return res.json({ user: user.toAuthJSON() });
      }
  
      return res.status(400).json({
          errors: {
              message: info,
          },
      });
  
      })(req, res, next);
  });
  </code></pre>
  
  <p>My '/admin' "logged in" route looks like this:</p>
  
  <pre><code>router.get("/admin", auth.required, function(req, res) {
      res.render('admin', {
          user : req.user // get the user out of session and pass to template
      });
  });
  </code></pre>
  
  <p>I'm not sure how I can redirect to my '/admin' route while also passing the token because currently I am seeing the following error after logging in. Makes sense since I am not passing the token to the '/admin' route...but how do I do that? :)</p>
  
  <pre><code>UnauthorizedError: No authorization token was found at middleware
  </code></pre>
  
  <p>Thanks in advance for the help!</p>
  
  <h2>EDIT:</h2>
  
  <p>Still can't figure this out and don't really understand how this flow is supposed to work...where do the headers need to be set to the token and how do I redirect to my admin page once the login is successful.</p>
  
  <p>Here is my middleware code if this helps:</p>
  
  <pre><code>const getTokenFromHeaders = (req) =&gt; {
  
      console.log("REQ: " + JSON.stringify(req.headers));
  
      const { headers: { authorization } } = req;
  
      if(authorization &amp;&amp; authorization.split(' ')[0] === 'Token') {
          return authorization.split(' ')[1];
      }
  
      return null;
  };
  
  const auth = {
      required: jwt({
          secret: 'secret',
          userProperty: 'payload',
          getToken: getTokenFromHeaders,
      }),
  
      optional: jwt({
          secret: 'secret',
          userProperty: 'payload',
          getToken: getTokenFromHeaders,
          credentialsRequired: false,
      }),
  };
  </code></pre>