For an Angular 7 app I am developing I'm using a HttpInterceptor to send the token with every request. It looks something like this:
export class JwtInterceptor implements HttpInterceptor {
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
let currentUser = JSON.parse(localStorage.getItem('currentUser'));
if (currentUser && currentUser.token) {
request = request.clone({
setHeaders: {
Authorization: `Bearer ${currentUser.token}`
}
});
}
return next.handle(request);
}
To turn this app into a progressive web app I am using @angular/service-worker and @angular/pwa. Configuration isn't hard I thought so it looks like it should work. But the problem is I can't find a way to enhance this worker with always sending the token with every request. I know localstorage isn't available in the worker, but that is not the problem right now. I first need to find a way like the HttpInterceptor works now. Is there a standard solution for this problem?