mirror of
https://github.com/hydralauncher/hydra.git
synced 2025-03-09 15:40:26 +00:00
feat: refactor
This commit is contained in:
parent
c4378c0ffc
commit
ffd3e37b48
1 changed files with 28 additions and 28 deletions
|
@ -216,22 +216,14 @@ export class HydraApi {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async refreshToken() {
|
public static async refreshToken() {
|
||||||
return this.instance
|
const { accessToken, expiresIn } = await this.instance
|
||||||
.post<{ accessToken: string; expiresIn: number }>(`/auth/refresh`, {
|
.post<{ accessToken: string; expiresIn: number }>(`/auth/refresh`, {
|
||||||
refreshToken: this.userAuth.refreshToken,
|
refreshToken: this.userAuth.refreshToken,
|
||||||
})
|
})
|
||||||
.then((response) => response.data);
|
.then((response) => response.data);
|
||||||
}
|
|
||||||
|
|
||||||
private static async revalidateAccessTokenIfExpired() {
|
|
||||||
const now = new Date();
|
|
||||||
|
|
||||||
if (this.userAuth.expirationTimestamp < now.getTime()) {
|
|
||||||
try {
|
|
||||||
const { accessToken, expiresIn } = await this.refreshToken();
|
|
||||||
|
|
||||||
const tokenExpirationTimestamp =
|
const tokenExpirationTimestamp =
|
||||||
now.getTime() +
|
Date.now() +
|
||||||
this.secondsToMilliseconds(expiresIn) -
|
this.secondsToMilliseconds(expiresIn) -
|
||||||
this.EXPIRATION_OFFSET_IN_MS;
|
this.EXPIRATION_OFFSET_IN_MS;
|
||||||
|
|
||||||
|
@ -251,6 +243,14 @@ export class HydraApi {
|
||||||
},
|
},
|
||||||
["id"]
|
["id"]
|
||||||
);
|
);
|
||||||
|
|
||||||
|
return { accessToken, expiresIn };
|
||||||
|
}
|
||||||
|
|
||||||
|
private static async revalidateAccessTokenIfExpired() {
|
||||||
|
if (this.userAuth.expirationTimestamp < Date.now()) {
|
||||||
|
try {
|
||||||
|
await this.refreshToken();
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
this.handleUnauthorizedError(err);
|
this.handleUnauthorizedError(err);
|
||||||
}
|
}
|
||||||
|
@ -265,7 +265,7 @@ export class HydraApi {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private static handleUnauthorizedError = (err) => {
|
private static readonly handleUnauthorizedError = (err) => {
|
||||||
if (err instanceof AxiosError && err.response?.status === 401) {
|
if (err instanceof AxiosError && err.response?.status === 401) {
|
||||||
logger.error(
|
logger.error(
|
||||||
"401 - Current credentials:",
|
"401 - Current credentials:",
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue