0

This is my code

canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
    let isAuthenticated: boolean = false
    this.authServiceLocal.isAuthenticated().then(response => isAuthenticated = response)

    if(isAuthenticated){
      return true
    }

    // not logged in so redirect to login page with the return url
    this.router.navigate(['/login'], { queryParams: { returnUrl: state.url }});
    return false

  }

I want to wait for the response from service then check the value of isAuthenticated I am getting true and false values from server as expected so no problem in api calling.

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Suhel
  • 927
  • 8
  • 19

1 Answers1

0

You can wait for the response and do the operation once the data is available.Change your code to the following

 canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise<any> {
        let isAuthenticated: boolean = false;
        let result = new EventEmitter<boolean>();
        return this.authServiceLocal.isAuthenticated().then(response => {isAuthenticated = response;
    if(isAuthenticated){

        }
        else {
             this.router.navigate(['/login'], { queryParams: { returnUrl: state.url }});
        result.emit(false);
         result.complete();

        }

        // not logged in so redirect to login page with the return url

    })
    return result;
      }
Vikhyath Maiya
  • 3,122
  • 3
  • 34
  • 68