After transitioning from using async/await to Observables in Angular, I am trying to refactor the following code snippet to make it work with Observables:
async refreshToken() {
const headers = this.authStorage.getRequestHeader();
const body = {
refreshToken: this.authStorage.getStoredValue('refreshToken'),
};
const newAccessToken = await this.http
.post<any>(ApiURLStore.REFRESH_TOKEN_URL, body, { headers: headers })
.toPromise();
this.authStorage.setValueToStore('accessToken', newAccessToken);
}
Essentially, I want to convert this function into one that returns an Observable after making a POST request to the backend and updating authStorage:
refreshToken(): Observable<any> {
const headers = this.authStorage.getRequestHeader();
const body = {
refreshToken: this.authStorage.getStoredValue('refreshToken'),
};
return this.http
.post<any>(ApiURLStore.REFRESH_TOKEN_URL, body, { headers: headers });
this.authStorage.setValueToStore('accessToken', newAccessToken);
}
While this code technically works, I'm facing issues accessing authStorage within an Observable. I've tried using pipe and subscribe without success. Any suggestions on how to approach this problem?