title: JsonWebTokenError: jwt must be a string
body: <p>I'm trying to authenticate on the Backend so that only the right user can get the correct data.</p>
  <p><strong>App.js</strong></p>
  <pre><code>const express = require('express');
  const app = express();
  const {mongoose} = require('./db/mongoose');
  const bodyParser = require('body-parser');
  const jwt = require('jsonwebtoken')
  
  //load Middleware
  app.use(bodyParser.urlencoded({ extended: true }));
  app.use(bodyParser.json());
  
  //CORS Middleware
  app.use(function(req, res, next) {
  req.header(&quot;Content-Type: application/x-www-form-urlencoded&quot;);
  res.header(&quot;Access-Control-Allow-Origin&quot;, &quot;*&quot;);
  res.header(&quot;Access-Control-Allow-Methods&quot;, &quot; GET, POST, OPTIONS, PUT, PATCH, DELETE&quot;)
  res.header(&quot;Access-Control-Allow-Headers&quot;, &quot;Origin, X-Requested-With, Content-Type, Accept, x-access-token, x-refresh-token&quot;);
  
  res.header(
      'Access-Control-Expose-Headers',
      'x-access-token, x-refresh-token'
  );
  
  next();
  })
  </code></pre>
  <p>To fulfil my goals I create the authenticate method</p>
  <pre><code>//aunthenticate middleware
  let authenticate = (res, req, next) =&gt; {
  let token = res.header('x-access-token');
  
  jwt.verify(token, User.getJWTSecret(), (err, decoded) =&gt; {
      if (err) {
          res.status(401).send(err);
          console.log(err)
      } else {
          req.user_id = decoded._id;
          next();
      }
  })
  }
  </code></pre>
  <p>I had to get in the user Model</p>
  <pre><code>const { User } = require('./db/models/users.model');
  </code></pre>
  <p>The point is every user should have a record of its own dispatch. Hence</p>
  <pre><code>app.get('/dispatch', authenticate, (req, res) =&gt; {
      Dispatch.find({
        _userId: req.user_id
      }).then((dispatch) =&gt; {
        res.send(dispatch);
      }).catch((e) =&gt; {
        res.send(e);
      })
  })
  </code></pre>
  <p>All the hints we have are in the</p>
  <p><strong>Users.model.js</strong></p>
  <pre><code>const mongoose = require(&quot;mongoose&quot;);
  const _ = require(&quot;lodash&quot;);
  const jwt = require(&quot;jsonwebtoken&quot;);
  const bcrypt = require(&quot;bcryptjs&quot;);
  const crypto = require(&quot;crypto&quot;)
  const jwtSecret = &quot;XXXXXXXXXXX&quot;;
  const UserSchema = new mongoose.Schema({
  email: {
      type: String,
      required: true,
      minlength: 1,
      trim: true,
      unique: true
  },
  password: {
      type: String, 
      required: true,
      minlength: 8
  },
  sessions: [{
      token: {
          type: String,
          required: true
      },
      expiresAt: {
          type: Number,
          required: true
      }
  }]
  });
  //Model Methods
  UserSchema.statics.getJWTSecret = () =&gt; {
    
    return jwtSecret;
  }
  
  const User = mongoose.model('User', UserSchema);
  
  module.exports = { User };
  </code></pre>
  <p>When I run the get dispatch method is postman the error on my terminal is
  jsonwebtokenError: jwt must be a string</p>