Why is a component not executed after a guard?

Viewed 36

I am implementing guards in my application, it works in a general way as follows:

  • Login page (username and password)
  • Token is created and stored
  • Tab1 is shown with a list of events. Clicking on any event opens a modal window that is a component with the specific information of that event.

These steps mentioned are the ideal scenario, but it happens that not, when clicking on a specific event it shows me the following error:

Can't bind to 'ngIf' since it isn't a known property of 'ion-content'.

Looking for the error, it is solved by adding CommonModule in components.module.ts, and of course it is added, otherwise the application would never have worked before implementing the guard.

Testing, I have to go to tab2 (events by category), go back to tab1, click on any event and it already shows as it should be...

What loads when I move to tab2?

Here some code:

app-routing.module.ts:

{
    path: 'main',
    loadChildren: () => import('./pages/tabs/tabs.module').then(m => m.TabsPageModule),
    canLoad:[UsuarioGuard]
  }

usuario.guard.ts:

constructor(private usuarioService:UsuarioService){}

  canLoad(): Observable<boolean> | Promise<boolean> | boolean {

    return this.usuarioService.validaToken();
  }

usuario.service.ts:

async cargarToken(){
    this.token = await this.storage.get('token') || null;
  }

  async validaToken():Promise<boolean>{

    await this.cargarToken();

    if(!this.token){
      this.navCtrl.navigateRoot('/login');
      return Promise.resolve(false);
    }

    return new Promise<boolean>(resolve => {

      const headers = new HttpHeaders({
        'Content-Type':'application/json',
        'Authorization': 'Bearer ' + this.token
      });

      this.http.get(`${URL}/API_3/userTkn`, { headers })
      .subscribe(resp => {
        if(resp['ok']){
          this.usuario = resp['usuario'];
          resolve(true);
        }
        else{
          this.navCtrl.navigateRoot('/login');
          resolve(false);
        }
      });
    });
  }

SOLUTION

Change canLoad[UsuarioGuard] for canActivate[UsuarioGuard]

0 Answers
Related