title: Token is not defined - MEAN Stack
body: <p>I've been following a book tutorial called mean machine. It has been super helpful. I am  setting up authentication and can't figure out this error I'm getting.</p>
  
  <p>Any help would be greatly appreciated! I can't seem to find the answer anywhere else.</p>
  
  <p>ERROR: token is not defined
      at Object.authTokenFactory.setToken (authService.js:69)</p>
  
  <p>authService.js:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>angular.module('authService', [])
  
  // ===================================================
  // auth factory to login and get information
  // inject $http for communicating with the API
  // inject $q to return promise objects
  // inject AuthToken to manage tokens
  // ===================================================
  
  .factory('Auth', function($http, $q, AuthToken) {
   // create auth factory obj
   var authFactory = {};
   // login user
   authFactory.login = function(username, password) {
    // return promise obj and its data
    return $http.post('/api/authenticate', {
     username: username,
     password: password
    })
    .success(function(data) {
     AuthToken.setToken(data.token);
     return data;
    });
   };
   
   // logout user by clearing token
   authFactory.logout = function() {
    AuthToken.setToken();
   };
   
   // check if user is logged in
   // checks for local token
   authFactory.isLoggedIn = function() {
    if (AuthToken.getToken())
     return true;
    else
     return false;
   };
   
   // get logged in user
   authFactory.getUser = function() {
    if (AuthToken.getToken())
     return $http.get('/api/me', { cache : true});
    else
     return $q.reject({ message : 'User has no token.'});
   };
   
   
   
   return authFactory;
  })
  // ===================================================
  // factory for handling tokens
  // inject $window to store token client-side
  // 
  // 
  // ===================================================
  .factory('AuthToken', function($window) {
   var authTokenFactory = {};
   
   // get token out of local storage
   authTokenFactory.getToken = function() {
    return $window.localStorage.getItem('token');
   };
   // function to set token or clear token
    // if a token is passed, set the token
    // if there is no token, clear it from local storage
    authTokenFactory.setToken = function() {
     if (token)
      $window.localStorage.setItem('token', token);
    else
     $window.localStorage.removeItem('token');
    };
   
   return authTokenFactory;
  })
  // ===================================================
  // application configuration to integrate token into requests
  // ===================================================
  .factory('AuthInterceptor', function($q, $location, AuthToken) {
   var interceptorFactory = {};
   
   // this will happen on all http requests
   interceptorFactory.request = function(config) {
    // grab token
    var token = AuthToken.getToken;
    // if token exists add it to the header as x-access-token
    if (token)
     config.headers['x-access-token'] = token;
     
     return config;
   };
   
   // happens on response errors
   interceptorFactory.responseError = function(response) {
    // if 403 from server
    if (response.status == 403) {
     AuthToken.setToken();
     $location.path('/login')
    }
    //return the errors from server as promise
    return $q.reject(response);
   };
   
   return interceptorFactory;
  });</code></pre>
  </div>
  </div>
  </p>
  
  <p>mainCtrl.js</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>angular.module('mainCtrl', [])
  .controller('MainController', function($rootScope, $location, Auth) {
   var vm = this;
   
   // get info if a person is logged in
   vm.loggedIn = Auth.isLoggedIn();
   
   // check to see if user is logged in on every req
   $rootScope.$on('$routeChangeStart', function() {
    vm.loggedIn = Auth.isLoggedIn();
    
    // get user info on route change
    // Auth.getUser()
    // .success(function(data) {
    //  vm.u = data;
    // });
    Auth.getUser().then(function (data) {
         vm.user = data;
    },
    function (response) {
     // Handle case where user is not logged in
     // or http request fails
    });
   });
   
   // handle login form
   vm.doLogin = function () {
    vm.processing = true;
    // clear error
    vm.error = '';
    
    // call Auth.login() func
    Auth.login(vm.loginData.username, vm.loginData.password)
     .success(function(data) {
      vm.processing = false;
      //if a user logs in, redirect to users pg
      if (data.success)
       $location.path('/users');
      else 
       vm.error = data.message;
      
      
     });
   };
   
   // log out
   vm.doLogOut = function() {
    Auth.logout();
    vm.u = {};
    $location.path('/login');
   };
  });</code></pre>
  </div>
  </div>
  </p>