Using UI-router, when a user goes to a page that require's a login, after they login, I would like to redirect them back to the original url they came from.
So for example,
/account/profile --> LOGIN PAGE (/login) --> /account/profile /someother/requiredLogin/path --> LOGIN PAGE (/login) --> /someother/requiredLogin/path My routes:
$stateProvider .state('accountProfile', { url: '/account/profile', data: { requiresLogin: true }, views: { '': { templateProvider: function($templateCache) { return $templateCache.get('templates/profile.html'); } } }, }) .state('anotherPage', { url: '/someother/path', data: { requiresLogin: true }, views: { '': { templateProvider: function($templateCache) { return $templateCache.get('templates/other.html'); } } }, }) Inside my application's run block I have:
.run(['$rootScope', '$state', 'LoginService', function($rootScope, $state, LoginService) { // Change title based on the `data` object in routes $rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams) { var requiresLogin = toState.data.requiresLogin; if (requiresLogin && !LoginService.check()) { event.preventDefault(); $state.go('login'); } }); } ]) As you can see, I do a basic job at adding a requiresLogin to each route, that just redirects always to login route. How can I keep track of the original URL and redirect them back to it AFTER they login?