i'm trying to protect my routes by implementing an asynchronous guard. To do so, I've created an AuthService with a method called isUserLoggedIn that consumes information from an API.
auth.service.ts
isUserLoggedIn(): Observable<boolean> {
return this.http.get<HttpApiResponse>(AppSettings.API_ENDPOING + 'auth/test').pipe(
map(response => response.data.logged)
);
}
And I have a guard that uses the service, if the user is logged in, the access is denied, but if the user is not logged in the access is granted.
export class AuthGuard implements CanActivate {
constructor(private auth: AuthService) { }
canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
return this.auth.isUserLoggedIn().pipe(
retry(3),
map(response => !response)
);
}
}
By here, everything is working properly. Now what I need is to grant the access to the user when the API is not responding or responds with error codes.