You have a problem in the promise callback then()
:
this.storage.get('authToken').then((token) => {
return something; // this won't work.
})
instead you can use from
, which will convert
your promise to an observable.
import { from, Observable } from 'rxjs';
import { map } from 'rxjs/operators';
public getOptions(): Observable<any> {
return from(this.storage.get('authToken')).pipe(map(token => {
return headers with token.
}));
}
So you could rewrite your code like this:
auth service:
private token: string | null = null;
public getOptions(): Observable<any> {
return this.getToken().pipe(
map(token => {
return {
headers: this.headers.set('Authorization', 'Bearer ' + token),
params: new HttpParams()
};
})
);
}
private getToken(): Observable<string | null> {
if (this.token) {
return of(this.token);
}
return from(this.storage.get('authToken')).pipe(
map(token => token?.authToken || null),
tap(token => this.token = token)
);
}
then you can use a switchmap:
getAllLiabilities(): Observable<Liability[]> {
return this.authService.getOptions().pipe(
switchMap(options => this.http.get<Liability[]>(this.url + 'me/', options))
);
}
Update
The reason for getting HttpEvent<T>
is because when the overload of .get()
receives an any
object it leaves the http event handling entirely up to you.
If you want it to return the provided element type, you have to satisfy the proper overload.
You can achieve that doing it like so:
Instead of returning the entire options, we only return the headers, which should be enough, because we do not really have enough to say about the rest of the options.
auth service
private token: string | null = null;
public createTokenHeaders(): Observable<HttpHeaders> {
const headers = new HttpHeaders();
return addToken(headers);
}
public addToken(headers: HttpHeaders): Observable<HttpHeaders> {
return this.getToken().pipe(
map(token => headers.set('Authorization', 'Bearer ' + (token || '')))
);
}
private getToken(): Observable<string | null> {
if (this.token) {
return of(this.token);
}
return from(this.storage.get('authToken')).pipe(
map(token => token?.authToken || null),
tap(token => this.token = token)
);
}
Then use it like so:
getAllLiabilities(): Observable<Liability[]> {
const url = this.url + 'me/';
const headers = new HttpHeaders();
return this.authService.addToken(headers).pipe(
switchMap(updatedHeaders => this.http.get<Liability[]>(url, { headers: updatedHeaders }))
);
}
or:
getAllLiabilities(): Observable<Liability[]> {
const url = this.url + 'me/';
return this.authService.createTokenHeaders().pipe(
switchMap(headers => this.http.get<Liability[]>(url, { headers }))
);
}
Note: Make sure you use the headers returned from the call to addToken. Reusing your own instantiated headers
will not work because setting a header always returns a new HttpHeaders
object. It is immutable.
StackBlitz Example