import axios from "axios"; import Cookies from "js-cookie"; import apiCall from "../system/apiCall"; import getApi from "../system/getAPI"; export async function signOut(disableRedirect: boolean = false): Promise<{ data: any }> { const storedSession = Cookies.get('ultimapanel-auth-session'); async function checkIfRemoved() { const savedSession = Cookies.get('ultimapanel-auth-session'); const savedUser = localStorage.getItem('ultimapanel-auth-jwt-session'); localStorage.removeItem('ultimapanel-auth-jwt-session'); Cookies.remove('ultimapanel-auth-session'); if (savedSession || savedUser) { setTimeout(async () => { await checkIfRemoved(); }, 1000); } } try { if (!storedSession) { throw 'You are not logged-in!'; } if (!(JSON.parse(storedSession as string)?.accessToken)) { throw 'You are not logged-in!'; } const response = (await apiCall.get( `${getApi()}/auth/signout`, { headers: { 'session': storedSession, } } )).data; Cookies.remove('ultima-latest-user'); Cookies.remove('ultimapanel-auth-session'); await checkIfRemoved(); window.dispatchEvent(new Event('ultimaCheckSession')); return { data: response }; } catch (error: any) { throw error; } finally { await checkIfRemoved(); if (disableRedirect) { window.location.reload(); } else { window.location.href = '/authorize'; } } };