Saturday, 27 May 2017

In Angular 2/4 why does my AuthGuard receive undefined from a function that is returning true? [duplicate]

This question already has an answer here:

My Authguard is receiving undefined from my function that is returning true.

My Authguard

@Injectable()
export class AuthGuardAdmin implements CanActivate{
constructor(private authService:AuthService, private router:Router){}

canActivate(){

 //console.log returns undefined
 console.log(this.authService.loggedInAdmin());

  if(this.authService.loggedInAdmin()){

    return true;
      } else {
         this.router.navigate(['/login']);
         return false;
        }
      }
  }

My logged in function. This function returns true, but the authguard gets undefined

loggedInAdmin(){

  this.getProfile().subscribe(profile => {
  this.user = profile.user, this.username = profile.user.username

    if (this.username=='pokeradmin'){

        //console.log returns true
        console.log(tokenNotExpired('id_token'));

        return tokenNotExpired('id_token');

     }else{
       console.log(false);
       return false;
     }

  });
}

I already debugged everything to see if it was a problem with the authservice, but it works fine. For some reason the loggedinadmin function is not returning true, but the console.log says its true



via harryd

No comments:

Post a Comment