Using a UI router, when a user goes to a page that requires a login, after logging in I would like to redirect them back to the original URL from which they came.
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');
}
}
},
})
In my application launch block, I have:
.run(['$rootScope', '$state', 'LoginService',
function($rootScope, $state, LoginService) {
$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 am doing the basic work of adding requiresLoginto each route, which simply redirects always to the route login. How can I track the source url and redirect it back after login?
source
share