title: JWT not decoding "JWT malformed" - Node Angular
body: <p>Upon logging in I send a JSON web token to the client-side. I have a custom authInterceptor which sends the JSON web token back to the server side.</p>
  
  <p>When I log in, everything works. Go to different sub-pages, works great. This is because I have a function which either checks for Passport authentication or token authentication, and upon logging in the Passport authentication works.  </p>
  
  <p>When I close the browser and return to the site, the JWT cannot decode. The JWT can decode when it is placed just under the encoding function. I have tried both the jwt-simple node module and the jsonwebtoken node modules, and I come back with the same error.</p>
  
  <p>This is my custom function which checks for a valid token:</p>
  
  <pre><code>function checkAuthentication(req, res, next){
    if (!req.headers.authorization) {
       return res.status(401).send({ message: 'Please make sure your request has an Authorization header' });
    }
    console.log("Here");
    var token = req.headers.authorization.split('.')[1];
    console.log(token);
    console.log(config.secret);
    var payload = null;
    try {
      console.log("And here....");
      payload = jwt.decode(token, config.secret);
      console.log(payload);
    }
    catch (err) {
      console.log(err);
      return false;
    }
  
    if (payload.exp &lt;= moment().unix()) {
      return false;
    }
    req.user = payload.sub;
    return true;
  }
  </code></pre>
  
  <p>jwt-simple uses <code>jwt.encode()</code> and <code>jwt.decode</code>, and jsonwebtoken uses <code>jwt.sign()</code> and <code>jwt.verify()</code>. This is what I get in my console:</p>
  
  <pre><code>Here
  eyJzdWIiOiI1NmEyZDk3MWQwZDg2OThhMTYwYTBkM2QiLCJleHAiOjE0NTYxOTEyNzQsImlhdCI6MTQ1NTMyNzI3NH0
  VerySecretPhrase
  And here....
  { [JsonWebTokenError: jwt malformed] name: 'JsonWebTokenError', message: 'jwt malformed' } 
  </code></pre>
  
  <p>This is the authInterceptor on the client-side. I collect the token and set it in the request header:</p>
  
  <pre><code>app.factory('httpInterceptor', function($q, $store, $window) {
  return {
      request: function (config){
          config.headers = config.headers || {};
          if($store.get('token')){
              var token = config.headers.Authorization = 'Bearer ' + $store.get('token');
          }
          return config;
      },
      responseError: function(response){
          if(response.status === 401 || response.status === 403) {
              $window.location.href = "http://localhost:3000/login";
          }
          return $q.reject(response);
      }
  };
  });
  </code></pre>