Ionic 2 Route Guards?

The new router v3 for Angular 2 is in beta, however i have noticed a few new things very cool, the one i want to discuss is route guards, basically providing a way to redirect or stop navigation if user is not logged in, and if it has permission to do so.

I’m basically asking for how does ionic face Authentication and Authorization.

You can achieve this sort of effect with custom attribute directives.

That would work with hiding part of the UI, but i’m asking about cancel and redirect routing.

I haven’t used this technique with Ionic yet, so I’m not certain about where to hook it into the lifecycle and navigation controller, but I believe the fundamental idea should translate.

Something like this:

<div gated>
... authorized-only content
  selector: '[gated]'
export class AuthenticationGatekeeper {
  constructor(nav:NavController, auther:Authenticator) {
    if (!auther.isAuthenticated()) {

…I have used in vanilla Angular applications before.


With that comes a little problem about transitions, if that push triggers before the transition animation finishes the navigation breaks, for me happens the same when i open an alert too soon.

As you’re actively involved in all the issues I’ve seen on this topic, there probably isn’t much I can say to you that you haven’t already seen, but hopefully those are implementation details that don’t interfere with the notion of using an attribute directive to achieve your goals, at least until the new router is usable with Ionic.

I guess i’ll have to stick with ionViewWillEnter page life cycle hook for the time being, however idk how would it play with transitions, i’ll test tomorrow.

Just to chime in on this, there is still a lot of working being done on the router from both ionic’s side and angular’s side. I’d hold off for now on things like this. We have some new features coming down the pipeline that will make this easy.

In my current situation i have this implemented as a global service that holds the login state, then in each component i check with the service what the login token is and if none, redirect to login, i hope in the future, instead of opening the page and then redirecting, i could check before and cancel or redirec to root instead of those 2 routing steps.

@mhartington Man can i just insert the new router with the IonicBootstrap() method? i’m about to start a project where route guards would shine like diamond, as far as i’ve read somewhere someone of the staff said the angular 2 router was deactivated because was under heavy dev changes.

Does that mean that if we try to use it Ionic blocks the router? or just that you guys don’t support it and thus we lose the navigation transitions when using it?

Hi @luchillo17,
for my last news ionic2 will never inlcude new angular2 router, but they will release their own router using urls. But this will happening only in a couple of weeks, probably for beta.12 or RC.1

Can you share your solution? There are many other users like us, which needs this as soon as possible.

We’re delaying the process, right now i want to test if putting the new router by my own in the IonicBootstrap() method would work, and how would that play with the styles of ionic components, off course if it does indeed work, since as i mentioned before i read that Ionic disabled the Angular router.

I don’t think it would work your way. But let us know your result! Thx

@mhartington Any updates on this ? Actually router Authorization is becoming really important according to the user roles
@luchillo17 Can you please share what worked for you ? I found somewhere that CanActivate can be used for this althogh still not sure, so anyone used this can give his feedbacks

[1] -
[2] - canActivate on ionic2?

@gaurav Sorry dude, not much to tell, right now we let the page transition and ionViewDidEnter we ask for the permissions, that’s just the other way around i would like it to work, i would like to have it ask before the navigation starts so the page only get’s open when the user does have the permission instead of opening then validating.

Idk if CanActivate works in Ionic since that one was from the deprecated router, also the Ionic router doesn’t make any mention about similar functionality in their router, i’ll have to test.

To manage Authentication and Authorization, I followed the post below and I wrote an interceptor of every http requests of my app to my server. In that way, if the server send back to the app “not logged in”, this will be catched and the user will then be redirected to the login screen.

The only difference I implemented in Ionic versus the link above is that instead of doing the redirect in the service itself I use an EventEmitter (from the service to the app root).

@reedrichards could you share your EventEmitter code please? thanks

Nothing special there

In the interceptor

requestIntercepted:EventEmitter<number> = new EventEmitter<number>();

And where you want to subscribe to the event

(<YourInterceptor> this.http).requestIntercepted.subscribe((errorCode: number) => this.doStuffsInCaseItsNotOk());

can we use the ionViewWillEnter() ?

For there record and since I can’t edit my EventEmitter code above, I just gonna add that here: it’s kind of a bad practice to use EventEmitter (except when it goes to components output). Therefore I change that EventEmitter code with an Observable like following:

private requestInterceptedSource: Subject<number> = new Subject<number>();
requestIntercepted:Observable<number> = this.requestInterceptedSource.asObservable();

The subscription part stay the same.