-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CanActivate analogue in ui-router-ng2 1.0.0? #2964
Comments
Using Transition Hooks. Start with a simple transition hook function which asynchronously checks if the user is authenticated. It redirects to the The simple auth hookfunction requireAuthentication(transition) {
let $state = transition.router.stateService;
let authSvc = transition.injector().get(AuthService);
return authSvc.checkAuthenticated().catch(() => $state.target('login'));
} Protect individual stateIf protecting a single state, use an .state({
name: 'foo',
onEnter: requireAuthentication
}) Protect classes of statesIf protecting a set or class of states, use a global hook which queries metadata on a state: .state({
name: 'foo',
protectMe: true
}); class MyUIRouterConfig {
configure(router: UIRouter) {
let criteria = { entering: (state) => state.protectMe };
router.transitionService.onBefore(criteria, requireAuthentication);
}
} Some other options: Protect classes of states using
|
Thank you for such a detailed answer! |
@kolkov please open a new issue. that sounds familiar and I think I know what the problem is.
Can you try Here's where we create the redirected transition options: https://github.com/angular-ui/ui-router/blob/master/src/transition/transition.ts#L374 |
|
return authSvc.checkAuthenticated().catch(() => $state.target('login')); is this a promise? what do i need to return here... |
@pscanlon1 in the example, |
How can we create protected accsess in new ui-router-ng2 similar as in standard Angular 2 router?
http://blog.thoughtram.io/angular/2016/07/18/guards-in-angular-2.html#defining-guards
The text was updated successfully, but these errors were encountered: