Guarding of routes on Ionic is it possible?

0

I'm currently doing security on my routes in ngOnInit, if it has on the localStorage the token key, then it stays on the Home route (My root route is the home). Otherwise, it redirects to the login screen. The problem is that it has a delay, the Home screen is displayed for a few milliseconds and then switches to the Login screen.

I have tried the / ngOnInit constructor and appcomponen.ts as soon as the platform is ready, but in this way I still have a blink on the screen.

Does anyone have a solution for this?

    
asked by anonymous 16.08.2018 / 20:44

1 answer

0

Yes, there is another better solution. You can read the Lifecycle events in documentation from IONIC. In your case, you can use ionViewCanEnter . Before the page is created, it will check its logic. If the user has access, his logic returns true and creates the page to be accessed, if it does not return false.

See this tutorial

import { Component } from '@angular/core';
import { NavController, NavParams } from 'ionic-angular';

@Component({
  selector: 'page-a',
  templateUrl: 'a.html'
})
export class PageA{

  constructor(public navCtrl: NavController) {}

  ionViewCanEnter() {
    \sua logica aqui. retorne true ou false.
  }
}
    
17.08.2018 / 20:04