Created
July 19, 2017 15:38
-
-
Save stevermeister/fb6f61786340af5891b8e59bf821e7ab 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
// src/app/auth/auth.service.ts | |
import { HttpRequest } from '@angular/common/http'; | |
// ... | |
export class AuthService { | |
cachedRequests: Array<HttpRequest<any>> = []; | |
public collectFailedRequest(request): void { | |
this.cachedRequests.push(request); | |
} | |
public retryFailedRequests(): void { | |
// retry the requests. this method can | |
// be called after the token is refreshed | |
} | |
} | |
// src/app/auth/jwt.interceptor.ts | |
// ... | |
intercept(request: 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) { | |
this.auth.collectFailedRequest(request); | |
} | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment