add refresh-token checker for api calls

This commit is contained in:
OxSaitama 2024-03-14 15:55:38 +01:00
parent da1de6f1e5
commit 2964ad1709

View File

@ -1,5 +1,8 @@
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";
import { NextResponse } from "next/server";
export enum ContentType { export enum ContentType {
JSON = "application/json", JSON = "application/json",
@ -44,6 +47,7 @@ export default abstract class BaseApiService {
} }
protected async getRequest<T>(url: URL, token?: string, contentType?: ContentType, ref?: IRef, fileName?: string) { protected async getRequest<T>(url: URL, token?: string, 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",
@ -53,6 +57,7 @@ export default abstract class BaseApiService {
} }
protected async postRequest<T>(url: URL, body: { [key: string]: unknown } = {}, token?: string) { protected async postRequest<T>(url: URL, body: { [key: string]: unknown } = {}, token?: string) {
await this.checkJwtToken();
return this.sendRequest<T>( return this.sendRequest<T>(
async () => async () =>
await fetch(url, { await fetch(url, {
@ -64,6 +69,7 @@ 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, {
@ -75,6 +81,7 @@ 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",
@ -86,6 +93,7 @@ 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",
@ -97,6 +105,7 @@ 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",
@ -108,6 +117,7 @@ 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",
@ -124,6 +134,42 @@ 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 NextResponse.redirect(new URL("/login"));
const now = Math.floor(Date.now() / 1000);
if (userDecodedToken.userId && userDecodedToken.exp < now) {
const refreshToken = CookieService.getInstance().getCookie("leCoffreRefreshToken");
if (!refreshToken) {
return NextResponse.redirect(new URL("/authorized-client"));
}
const decodedRefreshToken = jwt_decode(refreshToken) as IUserJwtPayload | ICustomerJwtPayload;
if (decodedRefreshToken.exp < now) {
return NextResponse.redirect(new URL("/authorized-client"));
}
await JwtService.getInstance().refreshToken(refreshToken);
}
if (userDecodedToken.userId && userDecodedToken.exp < now) {
const refreshToken = CookieService.getInstance().getCookie("leCoffreRefreshToken");
if (!refreshToken) {
return NextResponse.redirect(new URL("/id360/customer-callback"));
}
const decodedRefreshToken = jwt_decode(refreshToken) as IUserJwtPayload | ICustomerJwtPayload;
if (decodedRefreshToken.exp < now) {
return NextResponse.redirect(new URL("/id360/customer-callback"));
}
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);