title: Why jwt.sign function doesn't recognize my user as object?
body: <p>I m working on project and I found weird problem, jwt.sign function doesn't recognize passed user as object, but when I make new object inside jwt.sign then it works fine. </p>
  
  <p>If its hard to understand from text here is code:</p>
  
  <pre><code>var username = req.body.username;
  var password = req.body.password;
  
  User.getUserByUsername(username,(err, user)=&gt;{
      if(err) 
          throw err;
      if(!user) 
          return res.json({success: false, msg: "User with username "+ username+" doesn't exist"});
  
      User.comparePassword(password, user.password, (err, passwordsMatched) =&gt;{
          // Prints that user is type of object
          console.log("Type of user: "+typeof(user));
          if(err) throw err;
  
          if(passwordsMatched){
              // line bellow throws error that it expects object when I pass user
              var token = jwt.sign(user, Config.secret, {expiresIn: 604800});
              res.json({
                  success: true, 
                  token: 'JWT '+token, 
                  user: {
                      name : user.name, 
                      username: user.username 
                  }
              });
          }else{
              return res.json({success: false, msg: "Password doesn't match"});
          }
      });
  });
  </code></pre>
  
  <p>Line <code>var token = jwt.sign(user, Config.secret, {expiresIn: 604800});</code> throws following error: </p>
  
  <p>Error: Expected object
      at validate ...(Paths to jsonwebtoken and bcrypt modules + line where I call jwt.sign)</p>
  
  <p>So I tried to fix and I found out that if I create new object out of user data inside function call then it works, it looks like this:  </p>
  
  <pre><code>var token = jwt.sign({_id : user._id, 
                        username: user.username, 
                        password: user.password
                       }, 
                       Config.secret, 
                       {expiresIn: 604800}); 
  </code></pre>
  
  <p>So my question is why function doesn't accept my user objects and if there is solution to make it accept?</p>