diff --git a/src/front/Components/Layouts/LoginCallback/index.tsx b/src/front/Components/Layouts/LoginCallback/index.tsx index 2a22401f..735d3213 100644 --- a/src/front/Components/Layouts/LoginCallback/index.tsx +++ b/src/front/Components/Layouts/LoginCallback/index.tsx @@ -23,6 +23,7 @@ import Iframe from "src/sdk/Iframe"; import LoaderService from "src/common/Api/LeCoffreApi/sdk/Loader/LoaderService"; import { ProgressInfo } from "src/common/Api/LeCoffreApi/sdk/ImportData"; +import { FrontendVariables } from "@Front/Config/VariablesFront"; export default function LoginCallBack() { const router = useRouter(); @@ -44,7 +45,7 @@ export default function LoginCallBack() { resolve(UserStore.instance.getUser()); return; } - + // Poll for userInfo every 100ms const checkInterval = setInterval(() => { if (UserStore.instance.getUser()) { @@ -52,7 +53,7 @@ export default function LoginCallBack() { resolve(UserStore.instance.getUser()); } }, 100); - + // Timeout after 60 seconds setTimeout(() => { clearInterval(checkInterval); @@ -65,14 +66,22 @@ export default function LoginCallBack() { async function getUser() { UserStore.instance.disconnect(); - // TODO: review - // HACK: If start with http://local.lecoffreio.4nkweb:3000/authorized-client - // Replace with http://localhost:3000/authorized-client - if (window.location.href.startsWith('http://local.4nkweb.com')) { - window.location.href = window.location.href.replace('http://local.4nkweb.com:3000/authorized-client', 'http://localhost:3000/authorized-client'); - return; + const variables = FrontendVariables.getInstance(); + if (!variables?.ANK_BASE_REDIRECT_URI || !variables?.FRONT_APP_HOST) { + console.error("Redirect URIs not properly configured"); + } else if (window.location.href.startsWith(`${variables.ANK_BASE_REDIRECT_URI}/authorized-client`)) { + const newUrl = window.location.href.replace( + `${variables.ANK_BASE_REDIRECT_URI}/authorized-client`, + `${variables.FRONT_APP_HOST}/authorized-client` + ); + history.replaceState(null, '', newUrl); // Change l'URL sans rechargement } + // else if (window.location.href.startsWith(variables.ANK_BASE_REDIRECT_URI)) { + // window.location.href = window.location.href.replace(`${variables.ANK_BASE_REDIRECT_URI}/authorized-client`, `${variables.FRONT_APP_HOST}/authorized-client`); + // return; + // } + const code = router.query["code"]; if (code) { try { @@ -83,15 +92,15 @@ export default function LoginCallBack() { } const user: any = await Auth.getInstance().idNotAuth(code as string); - + // Extract both user data and auth token from the response const { idNotUser, authToken } = user; - + if (!authToken) { console.error('[LoginCallback] No authToken received from backend'); return router.push(Module.getInstance().get().modules.pages.Login.props.path + "?error=1"); } - + // Store the auth token for API authentication // TODO The authToken is just a uuid for now, it's very broken CookieService.getInstance().setCookie("leCoffreAccessToken", authToken); @@ -109,7 +118,7 @@ export default function LoginCallBack() { CookieService.getInstance().setCookie("leCoffreUserInfo", JSON.stringify(userInfoResponse.data)); setIsAuthModalOpen(true); console.log('[LoginCallback] authToken stored successfully'); - + return; } catch (e: any) { if (e.http_status === 401 && e.message === "Email not found") { @@ -217,7 +226,7 @@ export default function LoginCallBack() { // Office may not have a process too let collaboratorProcess: { processId: string, processData: { [key: string]: any } } | null = null; let officeProcess: { processId: string, processData: { [key: string]: any } } | null = null; - + // Initialize collaborator process try { // Wait for pairing ID to be available before proceeding @@ -225,7 +234,7 @@ export default function LoginCallBack() { console.log('[LoginCallback] Pairing ID obtained:', pairingId); // Check if we are part of the right collaborator process const myCollaboratorProcessesData = await MessageBus.getInstance().getProcessesDecoded((processId: string, values: { [key: string]: any }) => { - return values['utype'] === 'collaborator' + return values['utype'] === 'collaborator' && values['idNot'] === userInfo.idNot && values['isDeleted'] === 'false'; }); @@ -261,7 +270,7 @@ export default function LoginCallBack() { console.log('[LoginCallback] Pairing ID obtained:', pairingId); // Now we need to check for office process const myOfficeProcessesData = await MessageBus.getInstance().getProcessesDecoded((processId: string, values: { [key: string]: any }) => { - return values['utype'] === 'office' + return values['utype'] === 'office' && values['idNot'] === userInfo.office.idNot && values['isDeleted'] === 'false'; });