Angular Admin Guard problem An expression of type ‘void’ cannot be tested for truthiness

I have

public getUser(userId: string) {
return this.http.get<{
  _id: string;
  registrationStep: number;
  userType: string;
  isAdmin: boolean;
}>(BACKEND_URL+"getUserData/" + userId);


in my auth service. I want to read boolean value from database and set Admin guard to true.

I’m calling the function getIsAdmin from AdminGuard

getIsAdmin() {
this.getUser(this.getUserId()).subscribe(result => {
  return result.isAdmin;


And this is my Admin Guard code

    import {
} from "@angular/router";
import { Injectable } from "@angular/core";
import { Observable } from "rxjs";

import { AuthService } from "./auth.service";

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

    route: ActivatedRouteSnapshot,
    state: RouterStateSnapshot
  ): boolean | Observable<boolean> | Promise<boolean> {
    const isAdmin = this.authService.getIsAdmin();
    if (!isAdmin) {
    return isAdmin;

I get An expression of type ‘void’ cannot be tested for truthiness. Thank you for your help!


As others are pointing out you DO NOT need to subscribe. canActivate accepts an Observable<boolean> as a return type.

If you subscribe, the canActivate function will have already returned and you won’t get the value of the subscription back.

The following implementation should suffice:

// authService
getIsAdmin(): Observable<boolean> {
    return this.getUser(this.getUserId()).pipe(
        map(user => user.isAdmin)

And for the canActivate you want to do a custom redirect upon receiving false so you could:

canActivate(…): Observable<boolean> {
    const isAdmin$ = this.authService.getIsAdmin();
    return isAdmin$.pipe(
        tap(isAdmin => {
            if(!isAdmin) {

Leave a Reply

Your email address will not be published. Required fields are marked *