There is a working working example on how to redirect login. Firstly, 4 states are defined - 2 of them require authentication, the third one is public (for someone), and there is also an input state:
$stateProvider // available for anybody .state('public',{ url : '/public', template : '<div>public</div>', }) // just for authenticated .state('some',{ url : '/some', template : '<div>some</div>', data : {requiresLogin : true }, }) // just for authenticated .state('other',{ url : '/other', template : '<div>other</div>', data : {requiresLogin : true }, }) // the log-on screen .state('login',{ url : '/login', templateUrl : 'tpl.login.html', controller : 'UserCtrl', })
and it will be $stateChangeStart def, which is always redirected to login - if required ... only login itslef and public are always available / to anyone
.run(['$rootScope', '$state', 'User', function($rootScope, $state, User) { $rootScope.$on('$stateChangeStart' , function(event, toState, toParams, fromState, fromParams) { var isAuthenticationRequired = toState.data && toState.data.requiresLogin && !User.isLoggedIn ; if(isAuthenticationRequired) { event.preventDefault(); $state.go('login'); } }); }])
Make sure everything is in action here.
Radim KΓΆhler Jul 13 '14 at 17:08 2014-07-13 17:08
source share