There is a working example, how to redirect to login. Firstly there are 4 states defined - 2 of them require authentication, third is public (for anybody) and there is also login 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 that would be the $stateChangeStart
def, which always redirects to login
- if required ... only login
itslef and public
are available always/to anybody
.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');
}
});
}])
Check that all in action here
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…