Last active
April 18, 2018 10:47
-
-
Save chenkie/ca8344b685aa03b7d282a6c9632f0744 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
HttpInterceptor, | |
HttpRequest, | |
HttpResponse, | |
HttpHandler, | |
HttpEvent | |
} from '@angular/common/http'; | |
import 'rxjs/add/operator/map'; | |
@Injectable() | |
class JWTInterceptor implements HttpInterceptor { | |
constructor(private router: Router) {} | |
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { | |
return next.handle(req).map((event: HttpEvent<any>) => { | |
if (event instanceof HttpResponse) { | |
// do stuff with response if you want | |
} | |
}, (err: any) => { | |
if (err instanceof HttpErrorResponse { | |
if (err.status === 401) { | |
// redirect to login | |
} | |
} | |
}); | |
} | |
} |
changing
return next.handle(req).map((event: HttpEvent) => {
to
return next.handle(req).do((event: HttpEvent) => {
fixed it for me. Refer to https://angular.io/guide/http#intercepting-all-requests-or-responses
@Greg5ki, thanks for posting up the .map() => .do()
change, that worked perfectly for me (.map()
was only executing the first success arg, not the error).
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It does not work for me - while server returns 403 Forbidden it does not go to error callback.
Instead it triggers the successful callback and the event is typeof object (which is useless in reaction on error response). The object looks like this:
{type:0}
Could you help me with that, please?