Search code examples
angularangular-router-guardsangular-guards

When using an Angular route Guard: 'Observable<true | undefined>' is not assignable to type > 'Observable<boolean>'


I'm new to Angular.

How can I solve this problem?

I have installed Angular CLI: 11.0.7 and Node: 12.18.4

I have installed an Angular route guard:

ng g guard auth --skip-tests

The error:

Error: src/app/_guards/auth.guard.ts:15:5 - error TS2322: Type 'Observable<true | undefined>' is not assignable to type 'Observable'. Type 'boolean | undefined' is not assignable to type 'boolean'. Type 'undefined' is not assignable to type 'boolean'.

 15     return this.accountService.currentUser$.pipe(
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 16       map(user => {
    ~~~~~~~~~~~~~~~~~~~
...
 19       })
    ~~~~~~~~
 20     )
    ~~~~~
src/app/_guards/auth.guard.ts:16:11 - error TS7030: Not all code paths return a value.

16       map(user => {
             ~~~~~~~~~

guard

The Codes:

import { Injectable } from '@angular/core';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, UrlTree } from '@angular/router';
import { ToastrService } from 'ngx-toastr';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { AccountService } from '../_services/account.service';

@Injectable({
  providedIn: 'root'
})
export class AuthGuard implements CanActivate {
  constructor(private accountService: AccountService, private toastr: ToastrService) {}

  canActivate(): Observable<boolean> {
    return this.accountService.currentUser$.pipe(
      map(user => {
        if (user) return true; // the problem occurs here!
        this.toastr.error('You shall not pass!')
      })
    )
  }
  
}

Solution

  • Possibly because nothing is returned from the map operator when user is undefined. Try to return something

    export class AuthGuard implements CanActivate {
      constructor(private accountService: AccountService, private toastr: ToastrService) {}
    
      canActivate(): Observable<boolean> {
        return this.accountService.currentUser$.pipe(
          map(user => {
            if (user) return true;
            this.toastr.error('You shall not pass!');
            return false;
          })
        )
      }
    }
    

    Or better yet, you could use tap operator instead of map to perform side-effects like the 'toastr' notification.

    export class AuthGuard implements CanActivate {
      constructor(private accountService: AccountService, private toastr: ToastrService) {}
    
      canActivate(): Observable<boolean> {
        return this.accountService.currentUser$.pipe(
          tap(user => {
            if (!user) this.toastr.error('You shall not pass!')
          })
        )
      }
    }