title: Asymmetric keys with passport jwt. Verify always returns Unauthorized
body: <p>Working on an app, and I want security from the start, so I've created a private/public key pair, and I'm setting up <code>passport-jwt</code> like this: (<code>key</code> is the public part of the keypair)</p>
  
  <pre><code>(passport, key) =&gt; {
    const opts = {
      jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
      secretOrKey: key
    };
     passport.use(
       new JwtStrategy(opts, (payload, done) =&gt; {
         log.info({message: 'verifying the token', payload});
         User.findById(payload.id)
           .then(user =&gt; {
             if (user) {
               return done(null, {
                 id: user._id,
                 name: user.userName,
                 email: user.emailAddress
               });
             }
             log.info(payload);
             return done(null, false);
           })
           .catch(err =&gt; {
             log.error(err)
             return done('Unauthorized', false, payload);
            });
       })
     );
  };
  </code></pre>
  
  <p>and when the user logs in, I'm signing the token with the private key like this:</p>
  
  <pre><code>router.post('/login', (req, res) =&gt; {
          const email = req.body.email;
          const password = req.body.password;
  
          User.findOne({ email }).then(user =&gt; {
              if (!user) {
                  errors.email = 'No Account Found';
                  return res.status(404).json(errors);
              }
  
              bcrypt.compare(password, user.password).then(isMatch =&gt; {
                  if (isMatch) {
                      const payload = {
                          id: user._id,
                          name: user.userName,
                          email: user.emailAddress
                      };
                      log.info(payload);
                      jwt.sign(payload, private, { expiresIn: 30000000 }, (err, token) =&gt; {
                          if (err)
                              res.status(500).json({ error: 'Error signing token', raw: err });
                          // const refresh = uuid.v4();
                          res.json({ success: true, token: `Bearer ${token}` });
                      });
                  } else {
                      errors.password = 'Password is incorrect';
                      res.status(400).json(errors);
                  }
              });
          });
      });
  </code></pre>
  
  <p>I think there might be something that I'm missing, but I'm unsure what it could be.</p>
  
  <p>Also I've been generating the keys inside the app on initialization as well, using the following code.</p>
  
  <pre><code>const ensureKeys = () =&gt; {
      return new Promise((resolve, reject) =&gt; {
          ensureFolder('./keys').then(() =&gt; {
              /**
               * Ensure that both the private and public keys
               * are created, and if not create them both.
               * Never generate just a single key.
               */
              try {
                  if (
                      !fs.existsSync('./keys/private.key') &amp;&amp;
                      !fs.existsSync('./keys/public.key')
                  ) {
                      log.info('Keys do not exist. Creating them.');
                      diffHell.generateKeys('base64');
                      const public = diffHell.getPublicKey('base64');
                      const private = diffHell.getPrivateKey('base64');
                      fs.writeFileSync('./keys/public.key', public);
                      fs.writeFileSync('./keys/private.key', private);
                      log.info('keys created and being served to the app.');
                      resolve({ private, public });
                  } else {
                      log.info('keys are already generated. Loading from key files.');
                      const public = fs.readFileSync('./keys/public.key');
                      const private = fs.readFileSync('./keys/private.key');
                      log.info('keys loaded from files. Serving to the rest of the app.');
                      resolve({ private, public });
                  }
              } catch (e) {
                  log.error('issue loading or generating keys. Sorry.', e);
                  reject(e);
              }
          });
      });
  };
  </code></pre>