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 => { ~~~~~~~~~
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!')
})
)
}
}