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 | |
} | |
} | |
}); | |
} | |
} |
@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
@kuceraf
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