title: "ErrorTypeError: Cannot read property 'alg' of undefined" JWT
body: <p>I'm following a tutorial: <a href="https://github.com/eXtremeXR/APIAuthenticationWithNode/tree/Part_%2312" rel="nofollow noreferrer">https://github.com/eXtremeXR/APIAuthenticationWithNode/tree/Part_%2312</a></p>
  
  <p>And during the sign-up local part, there was an error  </p>
  
  <blockquote>
    <p>Unhandled promise rejections are deprecated.</p>
  </blockquote>
  
  <p>But I managed to solved that by using <strong>try-catch</strong> blocks. But again due to the main error:</p>
  
  <p><strong><code>TypeError: Cannot read property 'alg' of undefined</code></strong></p>
  
  <p>Every request I make returns me an error even though the registration is really successful.</p>
  
  <p>The error points me to this block:</p>
  
  <pre><code>signToken = user =&gt; {
    return JWT.sign({
      iss: 'CodeWorkr',
      sub: user.id,
      iat: new Date().getTime(), // current time
      exp: new Date().setDate(new Date().getDate() + 1) // current time + 1 day ahead
    }, JWT_SECRET);
  }
  </code></pre>
  
  <p>What's the cause of this and how do I solve it?</p>
  
  <hr>
  
  <p>Here's the full controller code:</p>
  
  <pre><code>const JWT = require('jws');
  const User = require('../models/user');
  const { JWT_SECRET } = require('../configuration');
  
  signToken = user =&gt; {
    return JWT.sign({
      iss: 'CodeWorkr',
      sub: user.id,
      iat: new Date().getTime(), // current time
      exp: new Date().setDate(new Date().getDate() + 1) // current time + 1 day ahead
    }, JWT_SECRET);
  }
  
  module.exports = {
    signUp: async (req, res, next) =&gt; {
      try {
        const { email, password } = req.body;
  
        // Check if there is a user with the same email
        const foundUser = await User.findOne({ "local.email": email });
        if (foundUser) { 
          return res.status(403).json({ error: 'Email is already in use'});
        }
  
        // Create a new user
        const newUser = new User({ 
          method: 'local',
          local: {
            email: email, 
            password: password
          }
        });
  
        await newUser.save();
  
        // Generate the token
        const token = signToken(newUser);
        // Respond with token
        res.status(200).json({ token });
      } catch (err) {
        res.status(500).send({
          message: "Error" + err
        })
      }
    },
  
    signIn: async (req, res, next) =&gt; {
      // Generate token
      const token = signToken(req.user);
      res.status(200).json({ token });
    },
  
    googleOAuth: async (req, res, next) =&gt; {
      // Generate token
      const token = signToken(req.user);
      res.status(200).json({ token });
    },
  
    facebookOAuth: async (req, res, next) =&gt; {
      // Generate token
      const token = signToken(req.user);
      res.status(200).json({ token });
    },
  
    secret: async (req, res, next) =&gt; {
      console.log('I managed to get here!');
      res.json({ secret: "resource" });
    }
  }
  </code></pre>