title: NextJs & Mongoose putting a jwt in database
body: <p>I tryed myself on a little NextJS App with Mongoose and JWT (json web token).
  Everything works and I can put user into my database and other stuff. Just when I create a JWT I can't put it into my database. Here is my code, grateful for every help :D</p>
  <p>First my schema for mongoose:</p>
  <pre><code>const mongoose = require('mongoose');
  
  const UserSchema = new mongoose.Schema({
      username: {
          type: String,
          required: [true, 'Please add a title'],
          unique: true,
          maxlength: [15, 'Username cannot be more than 15 characters']
      },
      password: {
          type: String,
          required: true,
          maxlength: [100, 'Password cannot be more than 100 characters'] 
      },
      jwt: {
          type: String,
          required: true,
          maxlength: [1000, 'JWT cannot be more than 200 characters'] 
      }
  })
  
  module.exports = mongoose.models.User || mongoose.model('User', UserSchema);
  </code></pre>
  <p>I don't know if type: String for jwt is correct, but JSON looks weird too and doesn't work.</p>
  <p>Now my backend API Code:</p>
  <pre><code>import dbConnect from '../../../utils/dbConnect';
  import User from '../../../models/User';
  import sjcl from 'sjcl';
  var jwt = require('jsonwebtoken');
  
  const hashword = &quot;censored&quot;
  
  dbConnect();
  
  export default async (req, res) =&gt; {
      
      var passwordtohash = req.body.password + hashword
      const BitArrayHash = sjcl.hash.sha256.hash(passwordtohash);
      const passwordhashed = sjcl.codec.hex.fromBits(BitArrayHash)
      var bodywithhash = req.body
      bodywithhash.password = passwordhashed
      const { method } = req;
      
      switch(method) {
          case 'POST':
              try {
                  const user = await User.find({&quot;username&quot;: bodywithhash.username, &quot;password&quot;: bodywithhash.password});
                  if (user.length &gt; 0) {
                      createJWT(user);
                      res.status(200).json({ success: true }) 
                  } else (res.status(200).json({ success: false}))           
              } catch (error) {
                  res.status(400).json({ success: false });
              }
              break;
          default:
              res.status(400).json({ success: false });
      }
  
  }
  
  async function createJWT(user) {
      jwt.sign({
          exp: Math.floor(Date.now() / 1000) + (60 * 60),
          data: 'foobar'
      }, 'secret')
      const iduser = { &quot;_id&quot; : user[0]._id.toString()}
      const updateuser = await User.findByIdAndUpdate(iduser, jwt);
  }
  </code></pre>
  <p>All my users in the database have a default value for JWT called &quot;default&quot;</p>