feat: token

This commit is contained in:
Nevo David 2025-05-16 15:35:40 +07:00
parent 31ddfab0b6
commit 5d908470f1
1 changed files with 27 additions and 12 deletions

View File

@ -1,3 +1,5 @@
'use client';
export interface Params {
baseUrl: string;
beforeRequest?: (url: string, options: RequestInit) => Promise<RequestInit>;
@ -14,21 +16,33 @@ export const customFetch = (
secured: boolean = true
) => {
return async function newFetch(url: string, options: RequestInit = {}) {
const loggedAuth = new URL(window.location.href).searchParams.get(
'loggedAuth'
);
const newRequestObject = await params?.beforeRequest?.(url, options);
const authNonSecuredCookie = typeof document === 'undefined' ? null : document.cookie
.split(';')
.find((p) => p.includes('auth='))
?.split('=')[1];
const authNonSecuredCookie =
typeof document === 'undefined'
? null
: document.cookie
.split(';')
.find((p) => p.includes('auth='))
?.split('=')[1];
const authNonSecuredOrg = typeof document === 'undefined' ? null : document.cookie
.split(';')
.find((p) => p.includes('showorg='))
?.split('=')[1];
const authNonSecuredOrg =
typeof document === 'undefined'
? null
: document.cookie
.split(';')
.find((p) => p.includes('showorg='))
?.split('=')[1];
const authNonSecuredImpersonate = typeof document === 'undefined' ? null : document.cookie
.split(';')
.find((p) => p.includes('impersonate='))
?.split('=')[1];
const authNonSecuredImpersonate =
typeof document === 'undefined'
? null
: document.cookie
.split(';')
.find((p) => p.includes('impersonate='))
?.split('=')[1];
const fetchRequest = await fetch(params.baseUrl + url, {
...(secured ? { credentials: 'include' } : {}),
@ -43,6 +57,7 @@ export const customFetch = (
? {}
: { 'Content-Type': 'application/json' }),
Accept: 'application/json',
...(loggedAuth ? { auth: loggedAuth } : {}),
...options?.headers,
...(auth
? { auth }