feat: refactor

This commit is contained in:
Zamitto 2025-01-15 16:57:25 -03:00
parent c4378c0ffc
commit ffd3e37b48

View File

@ -216,22 +216,14 @@ export class HydraApi {
}
public static async refreshToken() {
return this.instance
const { accessToken, expiresIn } = await this.instance
.post<{ accessToken: string; expiresIn: number }>(`/auth/refresh`, {
refreshToken: this.userAuth.refreshToken,
})
.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 =
now.getTime() +
Date.now() +
this.secondsToMilliseconds(expiresIn) -
this.EXPIRATION_OFFSET_IN_MS;
@ -251,6 +243,14 @@ export class HydraApi {
},
["id"]
);
return { accessToken, expiresIn };
}
private static async revalidateAccessTokenIfExpired() {
if (this.userAuth.expirationTimestamp < Date.now()) {
try {
await this.refreshToken();
} catch (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) {
logger.error(
"401 - Current credentials:",