I have an observable retrieving a token. I’m then saving that token into my local storage, then of which I’m calling into my interceptor. The issue is, when the local storage value is set inside the login observable, I can’t retrieve it. If I set a random token value outside of it, I can get it inside my interceptor. The issue is somehow retrieving that token value inside of my observable. I’ve also used this.storage.ready to wait until the local storage is ready and that doesn’t work either. I’m all out of ideas. Any help would be appreciated! I should also note that I’m just getting back null for the token value, even when logging in. I’m not using angular 4.3.1, I’m still using HttpModule from @angular/http.
auth.ts
login(userName: string, password: string, route: string = null): any {
this.storage.set('tokens', 'able to retrieve this token value inside interceptor');
this.doLogin(userName, password)
.subscribe(response => {
let token:string = JSON.stringify(response.access_token);
this.storage.set('token', token)
}
}
interceptor.ts
constructor(backend: ConnectionBackend, defaultOptions: RequestOptions, public storage: Storage) {
super(backend, defaultOptions);
}
request(url: string | Request, options?: RequestOptionsArgs): Observable<Response> {
return super.request(url, options);
}
get(url: string, options?: RequestOptionsArgs): Observable<Response> {
return Observable.fromPromise(
this.getRequestOptionArgs(options)
).mergeMap((options) => {
return super.get(url, options)
})
}
private getRequestOptionArgs(options?: RequestOptionsArgs) {
return this.storage.get('token').then((token) => {
console.log(token, 'token for the get')
if (options == null) {
options = new RequestOptions();
}
if (options.headers == null) {
options.headers = new Headers();
}
if (token !== null) {
options.headers.append('Authorization', 'Bearer ' + token);
}
options.headers.append('Content-Type', 'application/json');
return options;
});
}