Skip to content

Commit

Permalink
fix: cache-client token refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
Harasz committed Jun 22, 2022
1 parent 9a93d0e commit 05e9f1a
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions src/modules/cache-client/cache-client.service.ts
Expand Up @@ -76,12 +76,24 @@ export class CacheClient implements ICacheClient {
async authenticate() {
let tokens: AuthTokens | undefined = undefined;

const setTokens = () => {
if (!tokens) return;
if (!this.isBrowser) {
this._httpClient.defaults.headers.common[
'Authorization'
] = `Bearer ${tokens.token}`;
}
this.refresh_token = tokens.refreshToken;
};

// First try to refresh access token
try {
const refreshedTokens = await this.refreshToken();
tokens = refreshedTokens;
setTokens();

if (refreshedTokens && (await this.isAuthenticated())) {
tokens = refreshedTokens;
if (!(await this.isAuthenticated())) {
tokens = undefined;
}
} catch {
getLogger().error('[CACHE CLIENT] Failed to refresh tokens');
Expand All @@ -97,16 +109,9 @@ export class CacheClient implements ICacheClient {
});
this.pubKeyAndIdentityToken = pubKeyAndIdentityToken;
tokens = data;
setTokens();
}

// Set new tokens
if (!this.isBrowser) {
this._httpClient.defaults.headers.common[
'Authorization'
] = `Bearer ${tokens.token}`;
}
this.refresh_token = tokens.refreshToken;

// Run previously failed requests
console.log(
`[CACHE CLIENT] Running failed requests: ${this.failedRequests.length}`
Expand Down

0 comments on commit 05e9f1a

Please sign in to comment.