Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

typescript ionic 3 prepend call to all constructors

how can I call redirect if not logedIn in all pages constructors without repeating myself?

  constructor(protected appCtrl: AppController) {
    this.appCtrl.redirectIfNotLogedIn();
  }
like image 423
Abdulaziz Alsubaie Avatar asked Mar 31 '26 00:03

Abdulaziz Alsubaie


1 Answers

I suggest having a BaseComponent class that implements ionViewCanEnter that returns a boolean value.

Runs before the view can enter. This can be used as a sort of "guard" in authenticated views where you need to check permissions before the view can enter

export class BaseComponent{
  constructor(){}

  ionViewCanEnter(){
    //check if logged in or not and return the boolean value
  }

}

This component can be extended by all your pages.

  export class MyPage extends BaseComponent{
  //...
  }

You could try to call your this.appCtrl.redirectIfNotLogedIn(); in the constructor of BaseComponent but I suggest you use the NavGuard.

like image 79
Suraj Rao Avatar answered Apr 02 '26 13:04

Suraj Rao