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
.
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!')
})
)
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…