Coming from a background in AngularJS while learning Angular 5, observables are still causing some confusion for me.
I'm currently working on writing an HTTP interceptor for my authentication service. However, I'm struggling to properly return the next.handle(req)
method after retrieving a value from the localstorage
service (or any observable for that matter). I'm unsure of how to handle this using the subscribe method.
Below is the code I have at the moment (which is not functioning as expected):
@Injectable()
export class AuthinterceptorService implements HttpInterceptor {
constructor(private storage: LocalStorage) { }
intercept(req: HttpRequest<any>, next: HttpHandler):Observable<HttpEvent<any>> {
return this.storage.getItem('authToken').subscribe(res => {
return next.handle(req);
});
}
}
As you can see, I'm struggling with simply returning the Observable object within my asynchronous call without manipulating the data.
Thank you in advance for your assistance.