[BaseApiService] Remove unused checkJwtToken

This commit is contained in:
Sosthene 2025-09-11 09:01:17 +02:00
parent e6df9cbba0
commit 222b8dc503

View File

@ -1,7 +1,5 @@
import { FrontendVariables } from "@Front/Config/VariablesFront"; import { FrontendVariables } from "@Front/Config/VariablesFront";
import CookieService from "@Front/Services/CookieService/CookieService"; import CookieService from "@Front/Services/CookieService/CookieService";
import jwt_decode from "jwt-decode";
import JwtService, { ICustomerJwtPayload, IUserJwtPayload } from "@Front/Services/JwtService/JwtService";
export enum ContentType { export enum ContentType {
JSON = "application/json", JSON = "application/json",
@ -21,6 +19,8 @@ export default abstract class BaseApiService {
BaseApiService.baseUrl ??= BaseApiService.baseUrl ??=
this.variables.BACK_API_PROTOCOL + this.variables.BACK_API_PROTOCOL +
this.variables.BACK_API_HOST + this.variables.BACK_API_HOST +
':' +
this.variables.BACK_API_PORT +
this.variables.BACK_API_ROOT_URL + this.variables.BACK_API_ROOT_URL +
this.variables.BACK_API_VERSION; this.variables.BACK_API_VERSION;
} }
@ -45,8 +45,7 @@ export default abstract class BaseApiService {
return JSON.stringify(body); return JSON.stringify(body);
} }
protected async getRequest<T>(url: URL, token?: string, contentType?: ContentType, ref?: IRef, fileName?: string) { protected async getRequest<T>(url: URL, contentType?: ContentType, ref?: IRef, fileName?: string) {
await this.checkJwtToken();
const request = async () => const request = async () =>
await fetch(url, { await fetch(url, {
method: "GET", method: "GET",
@ -55,8 +54,7 @@ export default abstract class BaseApiService {
return this.sendRequest<T>(request, ref, fileName); return this.sendRequest<T>(request, ref, fileName);
} }
protected async postRequest<T>(url: URL, body: { [key: string]: unknown } = {}, token?: string) { protected async postRequest<T>(url: URL, body: { [key: string]: unknown } = {}) {
//await this.checkJwtToken();
return this.sendRequest<T>( return this.sendRequest<T>(
async () => async () =>
await fetch(url, { await fetch(url, {
@ -68,7 +66,6 @@ export default abstract class BaseApiService {
} }
protected async postRequestFormData<T>(url: URL, body: FormData) { protected async postRequestFormData<T>(url: URL, body: FormData) {
await this.checkJwtToken();
return this.sendRequest<T>( return this.sendRequest<T>(
async () => async () =>
await fetch(url, { await fetch(url, {
@ -80,7 +77,6 @@ export default abstract class BaseApiService {
} }
protected async putRequest<T>(url: URL, body: { [key: string]: unknown } = {}, token?: string) { protected async putRequest<T>(url: URL, body: { [key: string]: unknown } = {}, token?: string) {
await this.checkJwtToken();
const request = async () => const request = async () =>
await fetch(url, { await fetch(url, {
method: "PUT", method: "PUT",
@ -92,7 +88,6 @@ export default abstract class BaseApiService {
} }
protected async patchRequest<T>(url: URL, body: { [key: string]: unknown } = {}) { protected async patchRequest<T>(url: URL, body: { [key: string]: unknown } = {}) {
await this.checkJwtToken();
const request = async () => const request = async () =>
await fetch(url, { await fetch(url, {
method: "PATCH", method: "PATCH",
@ -104,7 +99,6 @@ export default abstract class BaseApiService {
} }
protected async deleteRequest<T>(url: URL, body: { [key: string]: unknown } = {}, token?: string) { protected async deleteRequest<T>(url: URL, body: { [key: string]: unknown } = {}, token?: string) {
await this.checkJwtToken();
const request = async () => const request = async () =>
await fetch(url, { await fetch(url, {
method: "DELETE", method: "DELETE",
@ -116,7 +110,6 @@ export default abstract class BaseApiService {
} }
protected async putFormDataRequest<T>(url: URL, body: FormData, token?: string) { protected async putFormDataRequest<T>(url: URL, body: FormData, token?: string) {
await this.checkJwtToken();
const request = async () => const request = async () =>
await fetch(url, { await fetch(url, {
method: "PUT", method: "PUT",
@ -133,41 +126,6 @@ export default abstract class BaseApiService {
return this.processResponse<T>(response, request, ref, fileName); return this.processResponse<T>(response, request, ref, fileName);
} }
private async checkJwtToken() {
const accessToken = CookieService.getInstance().getCookie("leCoffreAccessToken");
if (!accessToken) return;
const userDecodedToken = jwt_decode(accessToken) as IUserJwtPayload;
const customerDecodedToken = jwt_decode(accessToken) as ICustomerJwtPayload;
if (!userDecodedToken && !customerDecodedToken) return;
const now = Math.floor(Date.now() / 1000);
if (userDecodedToken.userId && userDecodedToken.exp < now) {
const refreshToken = CookieService.getInstance().getCookie("leCoffreRefreshToken");
if (!refreshToken) {
return;
}
const decodedRefreshToken = jwt_decode(refreshToken) as IUserJwtPayload | ICustomerJwtPayload;
if (decodedRefreshToken.exp < now) {
return;
}
await JwtService.getInstance().refreshToken(refreshToken);
}
if (customerDecodedToken.customerId && customerDecodedToken.exp < now) {
const refreshToken = CookieService.getInstance().getCookie("leCoffreRefreshToken");
if (!refreshToken) {
return;
}
const decodedRefreshToken = jwt_decode(refreshToken) as IUserJwtPayload | ICustomerJwtPayload;
if (decodedRefreshToken.exp < now) {
return;
}
await JwtService.getInstance().refreshToken(refreshToken);
}
return;
}
protected async processResponse<T>(response: Response, request: () => Promise<Response>, ref?: IRef, fileName?: string): Promise<T> { protected async processResponse<T>(response: Response, request: () => Promise<Response>, ref?: IRef, fileName?: string): Promise<T> {
let responseContent: T; let responseContent: T;
ref && (ref["response"] = response); ref && (ref["response"] = response);