0
votes

i write below code as my main page guard that use checkTokenValidation() to valid authentication token from user but my IDE show this error :A function whose declared type is neither 'void' nor 'any' must return a value. i return in my subscribe boolean but i don't know why i have this problem. i test my code and understand my code does not execute my return line as true or false and it is strange.

canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {

    this.authService.checkTokenValidation().subscribe((data) => {
      console.log(data);
      if (data['ok']) {
        this.router.navigate(['/overview']);
        return false;
      } else {
        return true;
      }
    });

  }
1
You are returnig in subscription but not in canActivate. Its a common rookie mistake. - Antoniossss

1 Answers

0
votes

By subscribing you are returning a Subscription instead of an Observable. You should map to the desired values and return an Observable:

canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
    return this.authService.checkTokenValidation().pipe(
      map( (data) => {
       if (data['ok']) {
         this.router.navigate(['/overview']);
         return false;
       } else {
         return true;
       }
     })
   );
}