title: NodeJs - Retrieve user information from JWT token?
body: <p>Node and Angular. I have a MEAN stack authentication application where I am setting a JWT token on successful login as follows, and storing it in a session in the controller. Assigning the JWT token to config.headers through service interceptor:</p>
  
  <pre><code>var token = jwt.sign({id: user._id}, secret.secretToken, { expiresIn: tokenManager.TOKEN_EXPIRATION_SEC });
              return res.json({token:token});
  </code></pre>
  
  <p>authservice.js Interceptor(omitted requestError,response and responseError):</p>
  
  <pre><code>authServices.factory('TokenInterceptor', ['$q', '$window', '$location','AuthenticationService',function ($q, $window, $location, AuthenticationService) {
          return {
              request: function (config) {
                  config.headers = config.headers || {};
                  if ($window.sessionStorage.token) {
                      config.headers.Authorization = 'Bearer ' + $window.sessionStorage.token;
                  }
                  return config;
              }               
          };
      }]);
  </code></pre>
  
  <p>Now I wanted to get the logged in user details from the token, How can I do that? I tried as follows, not working. When I log the error from Users.js file it's saying "ReferenceError: headers is not defined"</p>
  
  <p>authController.js:</p>
  
  <pre><code>$scope.me = function() {
      UserService.me(function(res) {
        $scope.myDetails = res;
      }, function() {
        console.log('Failed to fetch details');
        $rootScope.error = 'Failed to fetch details';
      })
    };
  </code></pre>
  
  <p>authService.js:</p>
  
  <pre><code>authServices.factory('UserService',['$http', function($http) {
    return {        
      me:function() {
      return $http.get(options.api.base_url + '/me');
      }
    }
  }]);
  </code></pre>
  
  <p>Users.js (Node):</p>
  
  <pre><code> exports.me = function(req,res){
      if (req.headers &amp;&amp; req.headers.authorization) {
          var authorization =req.headers.authorization;
          var part = authorization.split(' ');
          //logic here to retrieve the user from database
      }
      return res.send(200);
  }
  </code></pre>
  
  <p>Do i have to pass the token as a parameter too for retrieving the user details? Or save the user details in a separate session variable as well?</p>