0

When using HttpInterceptor and having 403 error, I refresh the token and navigate back to the previous state/router. The concept works fine till that point.

The issue is when the (previous) url-state is page/component with a form-fields. How can I keep the form data when an interceptor detect 403 error and goes back to this url-state?

intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    const router = this.injector.get(Router);

    return next.handle(request)
    .pipe(
        catchError((error: HttpErrorResponse) => {
            if ( error instanceof HttpErrorResponse ) {
                if ( error.status === 403 ) {
                    .....
                    router.navigate([ this.activatedRoute.snapshot['_routerState'].url ]);
                }
            }
            if (error.error instanceof ErrorEvent) {
                ....
            }
            return throwError(errMsg);
        })
    );
}
k.vincent
  • 3,743
  • 8
  • 37
  • 74
  • Is the goal to prefill a form with the latest data if it was in the last page visited before any api call that received a 403 response? – Massimiliano Sartoretto Mar 06 '19 at 15:20
  • Yes. To be more specific, let's say, user would like to register and enters data in form, clicks on submit button, then suddenly and exactly at that moment, token is expired and 403 occurs. Interceptor catches the error, refreshes token and send users back to register component. The previous form entries should not be lost. – k.vincent Mar 06 '19 at 15:26
  • Well that scenario describes a different thing. You can just refresh the token and repeat the same request without the user even noticing, no need to navigate back and forth. This is an example of such a behavior https://stackoverflow.com/a/46773408/4544288 – Massimiliano Sartoretto Mar 06 '19 at 15:30

0 Answers0