Skip to content
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

Lifecycle Callbacks #6

Closed
s001dxp opened this issue Nov 20, 2016 · 3 comments
Closed

Lifecycle Callbacks #6

s001dxp opened this issue Nov 20, 2016 · 3 comments

Comments

@s001dxp
Copy link

s001dxp commented Nov 20, 2016

I want to be able to check if a user is authenticated and/or authorized to visit a view state before each transition to a new view/state. The default angular router has a 'canActivate' feature that is run before the app transitions to a new view.

How can I achieve the same using ui-router? I've searched the docs, the codebase here, and the web but have not found anything.

Thanks for your help

@chaoyangnz
Copy link

You can use resolve in the state declaration or register a callback of TransitionService: onStart(..), onEntering(..), ...

@christopherthielen
Copy link
Member

christopherthielen commented Jan 21, 2017

As @richdyang mentioned, use (for example) an onStart hook

You can make a global hook which is triggered based on metadata stored on the state.

let hookMatchCriteria = { 
  to: state => state.data && state.data.requiresAuth 
};
router.transitionService.onStart(hookMatchCriteria, function hook(transition: Transition) {
  userSvc: UserService = transition.injector().get(UserService);
  if (!userSvc.authenticated()) {
    return router.stateService.target('login');
  }
});

@kolkov
Copy link

kolkov commented Feb 24, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants