Fix for SDK - Persistent login refresh not working (#4113)

This commit is contained in:
Pyll Gomez
2021-02-16 18:11:39 -05:00
committed by GitHub
parent ee96c99ec5
commit 4250c47e04

View File

@@ -41,7 +41,7 @@ export class AuthHandler {
this.accessTokenInitId = this.axios.interceptors.request.use((config) => this.initializeAccessToken(config));
if (this.autoRefresh) {
this.refresh();
this.refresh(true);
}
}
@@ -75,7 +75,7 @@ export class AuthHandler {
await this.storage.setItem('directus_refresh_token', data.refresh_token);
}
if (this.autoRefresh) {
this.refresh();
this.refresh(true);
}
return response.data;
@@ -84,15 +84,17 @@ export class AuthHandler {
/**
* Refresh access token 10 seconds before expiration
*/
async refresh(): Promise<{ data: AuthResponse } | undefined> {
async refresh(isInitialInvoke: Boolean): Promise<{ data: AuthResponse } | undefined> {
this.removeTimeout();
this.expiresAt = await this.storage.getItem('directus_access_token_expires');
if (!this.expiresAt) return;
if (Date.now() + 10000 < this.expiresAt && this.autoRefresh) {
this.autoRefreshTimeout = setTimeout(() => this.refresh(), this.expiresAt - Date.now() - 10000);
return;
this.autoRefreshTimeout = setTimeout(() => this.refresh(false), this.expiresAt - Date.now() - 10000);
if (!isInitialInvoke) {
return;
}
}
const payload: Record<string, any> = { mode: this.mode };
@@ -118,7 +120,7 @@ export class AuthHandler {
}
if (this.autoRefresh) {
this.autoRefreshTimeout = setTimeout(() => this.refresh(), data.expires - 10000);
this.autoRefreshTimeout = setTimeout(() => this.refresh(false), data.expires - 10000);
}
return response.data;
}