https://i.sstatic.net/9lewp.pngImplementing authGuard for my Routers has been a challenge. Whenever I try to handle error codes, it ends up breaking the entire application. I'm currently at a loss on how to resolve this issue and would greatly appreciate any assistance.
Below is the code snippet for My AuthGuard:
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
return this._auth.validate()
.map((isValidated) => {
if (isValidated && localStorage.getItem('authToken') && this.checkCookie()) {
return true;
}
this.router.navigate(['/login'], { queryParams: { returnUrl: state.url } });
return false;
});
}
And here is part of My Auth Service:
@Injectable()
export class AuthService {
loginStatus;
constructor(private http: Http, private _apiEndPoint: ApiEndPoint, private router: Router) { }
validate() {
return this.http.get(this._apiEndPoint.validate)
.map(
(response: Response) => {
const result = response.json();
// Check authentication status from response
if (result.authenticated) {
return true;
} else {
return false;
}
})
.catch(error => Observable.throw(error));
}
}
I am encountering an error: "Uncaught (in promise): Response with status: 401 Unauthorised for URL."
https://i.sstatic.net/9lewp.png
Whenever this error occurs, my page just goes completely blank.