-1

I have a question about, in Angular 8 How can I hide the line of http method server link

auth-interceptor.ts

  private handleAuthError(err: HttpErrorResponse): Observable<any> {
    let errorMsg;
    if (err.status === 401 || err.status === 403) {
      errorMsg = err.error.message;
      this.injector.get(Router).navigateByUrl(`/login`);
    }
    return throwError(errorMsg);
  }

  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    const token = localStorage.getItem("id_token");
    if (token) {
      const authReq = req.clone({ headers: req.headers.set("Authorization", "Bearer " + token) });
      return next.handle(authReq).pipe(catchError(err => this.handleAuthError(err)));
    }
    else {
      return next.handle(req);
    }
  }

GET http://localhost:3000/api/toto 401 (Unauthorized) -- this line

ERROR Auth failed!*

1 Answer 1

1

I think you should use try catch block and don't throw error of server to end user.

With this practice you can warn user in any way you want.

And if you don't want write your error message then use:

private handleAuthError(err: HttpErrorResponse): Observable<any> {
  let errorMsg;
  if (err.status === 401 || err.status === 403) {
    errorMsg = err.error.message;
    this.injector.get(Router).navigateByUrl(`/login`);
  }
  errorMsg.replace(Url, '');
  return throwError(errorMsg);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.