{"action":"opened","issue":{"url":"https://api.github.com/repos/DaftMonk/generator-angular-fullstack/issues/1398","labels_url":"https://api.github.com/repos/DaftMonk/generator-angular-fullstack/issues/1398/labels{/name}","comments_url":"https://api.github.com/repos/DaftMonk/generator-angular-fullstack/issues/1398/comments","events_url":"https://api.github.com/repos/DaftMonk/generator-angular-fullstack/issues/1398/events","html_url":"https://github.com/DaftMonk/generator-angular-fullstack/issues/1398","id":114405548,"number":1398,"title":"How does Auth redirect work?","user":{"login":"akcasoy","id":12121451,"avatar_url":"https://avatars.githubusercontent.com/u/12121451?v=3","gravatar_id":"","url":"https://api.github.com/users/akcasoy","html_url":"https://github.com/akcasoy","followers_url":"https://api.github.com/users/akcasoy/followers","following_url":"https://api.github.com/users/akcasoy/following{/other_user}","gists_url":"https://api.github.com/users/akcasoy/gists{/gist_id}","starred_url":"https://api.github.com/users/akcasoy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/akcasoy/subscriptions","organizations_url":"https://api.github.com/users/akcasoy/orgs","repos_url":"https://api.github.com/users/akcasoy/repos","events_url":"https://api.github.com/users/akcasoy/events{/privacy}","received_events_url":"https://api.github.com/users/akcasoy/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2015-10-31T11:41:13Z","updated_at":"2015-10-31T11:41:13Z","closed_at":null,"body":"A call to the URL \"http://domain:9000/admin\" redirects users to login page. After login, every URL will be redirected to a predefined URL (\"/\" or \"/xxx\"). How can i improve this so that the authentication interceptor should be aware of the original URL and make a redirect to that location?\r\n\r\nI did not quite understand the auth service logic flow. I have these relevant files and they are mostly as generated:\r\n\r\naccount.js:\r\n\r\n```javascript\r\nangular.module('teamtoolApp')\r\n  .config(function ($stateProvider) {\r\n    $stateProvider\r\n      .state('admin', {\r\n        url: '/admin',\r\n        templateUrl: 'app/account/admin/admin.html',\r\n        controller: 'AdminCtrl',\r\n        authenticate: true\r\n      })\r\n```\r\n\r\napp.js\r\n\r\n```javascript\r\nangular.module('teamtoolApp', [\r\n  ....\r\n])\r\n  .config(function ($stateProvider, $urlRouterProvider, $locationProvider, $httpProvider) {\r\n    $urlRouterProvider\r\n      .otherwise('/');\r\n\r\n    $locationProvider.html5Mode(true);\r\n    $httpProvider.interceptors.push('authInterceptor');\r\n  })\r\n\r\n  .factory('authInterceptor', function ($rootScope, $q, $cookies, $location) {\r\n    return {\r\n      // Add authorization token to headers\r\n      request: function (config) {\r\n        config.headers = config.headers || {};\r\n        if ($cookies.get('token')) {\r\n          config.headers.Authorization = 'Bearer ' + $cookies.get('token');\r\n        }\r\n        return config;\r\n      },\r\n\r\n      // Intercept 401s and redirect you to login\r\n      responseError: function(response) {\r\n        if(response.status === 401) {\r\n          $location.path('/login');\r\n          // remove any stale tokens\r\n          $cookies.remove('token');\r\n          return $q.reject(response);\r\n        }\r\n        else {\r\n          return $q.reject(response);\r\n        }\r\n      }\r\n    };\r\n  })\r\n\r\n  .run(function ($rootScope, $location, Auth) {\r\n    // Redirect to login if route requires auth and you're not logged in\r\n    $rootScope.$on('$stateChangeStart', function (event, next) {\r\n      Auth.isLoggedInAsync(function(loggedIn) {\r\n        if (next.authenticate && !loggedIn) {\r\n          $location.path('/login');\r\n        }\r\n      });\r\n    });\r\n  });\r\n```\r\n\r\nauth.service.js\r\n\r\n```javascript\r\nangular.module('teamtoolApp')\r\n  .factory('Auth', function Auth($location, $rootScope, $http, User, $cookies, $q) {\r\n    var currentUser = {};\r\n    if($cookies.get('token')) {\r\n      currentUser = User.get();\r\n    }\r\n\r\n    return {\r\n\r\n      /**\r\n       * Authenticate user and save token\r\n       *\r\n       * @param  {Object}   user     - login info\r\n       * @param  {Function} callback - optional\r\n       * @return {Promise}\r\n       */\r\n      login: function(user, callback) {\r\n        var cb = callback || angular.noop;\r\n        var deferred = $q.defer();\r\n\r\n        var expireDate = new Date();\r\n        expireDate.setDate(expireDate.getDate() + 365); // in one year\r\n\r\n        $http.post('/auth/local', {\r\n          email: user.email,\r\n          password: user.password\r\n        }).\r\n        success(function(data) {\r\n          $cookies.put('token', data.token, {'expires': expireDate});\r\n          currentUser = User.get();\r\n          deferred.resolve(data);\r\n          return cb();\r\n        }).\r\n        error(function(err) {\r\n          this.logout();\r\n          deferred.reject(err);\r\n          return cb(err);\r\n        }.bind(this));\r\n\r\n        return deferred.promise;\r\n      },\r\n\r\n      /**\r\n       * Delete access token and user info\r\n       *\r\n       * @param  {Function}\r\n       */\r\n      logout: function() {\r\n        $cookies.remove('token');\r\n        currentUser = {};\r\n      },\r\n\r\n      /**\r\n       * Create a new user\r\n       *\r\n       * @param  {Object}   user     - user info\r\n       * @param  {Function} callback - optional\r\n       * @return {Promise}\r\n       */\r\n      createUser: function(user, callback) {\r\n        var cb = callback || angular.noop;\r\n\r\n        return User.save(user,\r\n          function(err) {\r\n            this.logout();\r\n            return cb(err);\r\n          }.bind(this)).$promise;\r\n      },\r\n\r\n      /**\r\n       * Change password\r\n       *\r\n       * @param  {String}   oldPassword\r\n       * @param  {String}   newPassword\r\n       * @param  {Function} callback    - optional\r\n       * @return {Promise}\r\n       */\r\n      changePassword: function(oldPassword, newPassword, callback) {\r\n        var cb = callback || angular.noop;\r\n\r\n        return User.changePassword({ id: currentUser._id }, {\r\n          oldPassword: oldPassword,\r\n          newPassword: newPassword\r\n        }, function(user) {\r\n          return cb(user);\r\n        }, function(err) {\r\n          return cb(err);\r\n        }).$promise;\r\n      },\r\n\r\n      /**\r\n       * Gets all available info on authenticated user\r\n       *\r\n       * @return {Object} user\r\n       */\r\n      getCurrentUser: function() {\r\n        return currentUser;\r\n      },\r\n\r\n      /**\r\n       * Check if a user is logged in\r\n       *\r\n       * @return {Boolean}\r\n       */\r\n      isLoggedIn: function() {\r\n        return currentUser.hasOwnProperty('role');\r\n      },\r\n\r\n      /**\r\n       * Waits for currentUser to resolve before checking if user is logged in\r\n       */\r\n      isLoggedInAsync: function(cb) {\r\n        if(currentUser.hasOwnProperty('$promise')) {\r\n          currentUser.$promise.then(function() {\r\n            cb(true);\r\n          }).catch(function() {\r\n            cb(false);\r\n          });\r\n        } else if(currentUser.hasOwnProperty('role')) {\r\n          cb(true);\r\n        } else {\r\n          cb(false);\r\n        }\r\n      },\r\n\r\n      /**\r\n       * Check if a user is an admin\r\n       *\r\n       * @return {Boolean}\r\n       */\r\n      isAdmin: function() {\r\n        return currentUser.role === 'admin';\r\n      },\r\n\r\n      /**\r\n       * Get auth token\r\n       */\r\n      getToken: function() {\r\n        return $cookies.get('token');\r\n      }\r\n    };\r\n  });\r\n```"}}