Skip to content

Commit

Permalink
Merge pull request #532 from energywebfoundation/task/IS-39-fix-undef…
Browse files Browse the repository at this point in the history
…ined-token

fix: avoid refreshing undefined token
  • Loading branch information
Harasz authored Apr 21, 2022
2 parents a7451e3 + 86bed3c commit 9dc432c
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions src/modules/cache-client/cache-client.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,18 @@ export class CacheClient implements ICacheClient {
*/
async authenticate() {
try {
const { refreshToken, token } = await this.refreshToken();
if (!this.isBrowser) {
this.httpClient.defaults.headers.common[
'Authorization'
] = `Bearer ${token}`;
}
if (await this.isAuthenticated()) {
this.refresh_token = refreshToken;
return;
const refreshedTokens = await this.refreshToken();

if (refreshedTokens) {
if (!this.isBrowser) {
this.httpClient.defaults.headers.common[
'Authorization'
] = `Bearer ${refreshedTokens.token}`;
}
if (await this.isAuthenticated()) {
this.refresh_token = refreshedTokens.refreshToken;
return;
}
}
} catch {
// Ignore errors
Expand Down Expand Up @@ -384,10 +387,15 @@ export class CacheClient implements ICacheClient {
return data;
}

private async refreshToken(): Promise<{
token: string;
refreshToken: string;
}> {
private async refreshToken(): Promise<
| {
token: string;
refreshToken: string;
}
| undefined
> {
if (!this.refresh_token) return undefined;

const { data } = await this.httpClient.get<{
token: string;
refreshToken: string;
Expand Down

0 comments on commit 9dc432c

Please sign in to comment.