title: JSONWebTokenError: JWT Malformed at index.js
body: <p>I'll be honest, I'm totally lost on this. This project was working perfectly fine, and I'm running this from my last working GitHub push. Here's what happened. I lost my entire backend. Don't know how, not sure what happened. So when I pulled from GitHub I didn't have a backup of my JWT secret key. I got the "Invalid Token Error" (or something like that). I put this whole project on a new server/database and now I get the following error:</p>
  
  <pre><code>JsonWebTokenError: jwt malformed
      at Object.module.exports [as verify] (C:\Users\glove\workspace\CharityApp\backend\node_modules\jsonwebtoken\verify.js:49:17)
      at C:\Users\glove\workspace\CharityApp\backend\src\index.js:15:30
      at Layer.handle [as handle_request] (C:\Users\glove\workspace\CharityApp\backend\node_modules\express\lib\router\layer.js:95:5)
      at trim_prefix (C:\Users\glove\workspace\CharityApp\backend\node_modules\express\lib\router\index.js:317:13)
      at C:\Users\glove\workspace\CharityApp\backend\node_modules\express\lib\router\index.js:284:7
      at Function.process_params (C:\Users\glove\workspace\CharityApp\backend\node_modules\express\lib\router\index.js:335:12)
      at next (C:\Users\glove\workspace\CharityApp\backend\node_modules\express\lib\router\index.js:275:10)
      at cookieParser (C:\Users\glove\workspace\CharityApp\backend\node_modules\cookie-parser\index.js:71:5)
      at Layer.handle [as handle_request] (C:\Users\glove\workspace\CharityApp\backend\node_modules\express\lib\router\layer.js:95:5)
      at trim_prefix (C:\Users\glove\workspace\CharityApp\backend\node_modules\express\lib\router\index.js:317:13)
  </code></pre>
  
  <p>And the block of code it's referring to in index.js:</p>
  
  <pre class="lang-js prettyprint-override"><code>const jwt = require('jsonwebtoken');
  const cookieParser = require('cookie-parser');
  require('dotenv').config({path: 'variables.env'});
  const createServer = require('./createServer');
  const db = require('./db');
  
  const server = createServer();
  
  server.express.use(cookieParser());
  
  // decode jwt to get user id on each request
  server.express.use((req, res, next) =&gt; {
      const {token} = req.cookies;
      if(token) {
          const {userId} = jwt.verify(token, process.env.APP_SECRET);
          // put userid onto request for access
          req.userId = userId;
      }
      next();
  });
  </code></pre>
  
  <p>And the sign code that's used multiple times whintin GraphQL resolvers involving signing in.</p>
  
  <pre class="lang-js prettyprint-override"><code>const token = jwt.sign({userId: user.id}, process.env.APP_SECRET);
      // set cookie with token
      ctx.response.cookie("token", token, {
          httpOnly: true,
          maxAge
  });
  </code></pre>
  
  <p>The app secret just pulls from a .env file and the maxAge is just a simple const for a 1-year cookie.</p>
  
  <p>This was basically a finished project. :/ If someone can help me out on it, I'd really appreciate it.</p>