From 7e73398e5b359fc6d03f465621f128cdeafaee62 Mon Sep 17 00:00:00 2001 From: admtracksteel Date: Mon, 20 Apr 2026 20:58:29 +0000 Subject: [PATCH] fix: pass full url to handleSignInCallback --- dist/index.html | 2 +- src/main.js | 59 ++++++++++++++----------------------------------- 2 files changed, 18 insertions(+), 43 deletions(-) diff --git a/dist/index.html b/dist/index.html index 71bd39a..ee76abd 100644 --- a/dist/index.html +++ b/dist/index.html @@ -6,7 +6,7 @@ Otimizador de Corte - TSCUT - + diff --git a/src/main.js b/src/main.js index ed621fa..3ab7358 100644 --- a/src/main.js +++ b/src/main.js @@ -7,65 +7,40 @@ const style = document.createElement('style'); style.innerHTML = 'body { display: none !important; }'; document.head.appendChild(style); -// Implementação de Storage Persistente para evitar perdas em redirecionamentos -const persistentStorage = { - getItem: (key) => localStorage.getItem(key), - setItem: (key, value) => localStorage.setItem(key, value), - removeItem: (key) => localStorage.removeItem(key), -}; - const logtoClient = new LogtoClient({ endpoint: import.meta.env.VITE_LOGTO_ENDPOINT, appId: import.meta.env.VITE_LOGTO_APP_ID, scopes: ['openid', 'offline_access', 'profile', 'email', 'organizations'], - storage: persistentStorage, // Força o uso de localStorage }); async function protectPage() { const urlParams = new URLSearchParams(window.location.search); const isCallback = urlParams.has('code') && urlParams.has('state'); - // Forçar a URI exata registrada no Logto para evitar variações de origin - const exactRedirectUri = 'https://tscut.reifonas.cloud/'; - - console.log('TSCUT Auth: Inicializando...', { - isCallback, - href: window.location.href, - storageKeys: Object.keys(localStorage).filter(k => k.includes('logto')) - }); + // A URI exata para a qual o Logto vai devolver o usuário + const redirectUri = window.location.origin + '/'; if (isCallback) { try { - console.log('TSCUT Auth: Processando callback com URI:', exactRedirectUri); + // CRÍTICO: handleSignInCallback precisa da URL COMPLETA para extrair o ?code= e &state= + await logtoClient.handleSignInCallback(window.location.href); - // Pequeno delay para garantir que o storage foi persistido pelo navegador - await new Promise(r => setTimeout(r, 100)); + console.log('TSCUT Auth: Login bem sucedido!'); + // Limpa os parâmetros da URL para evitar erro no reload manual + window.history.replaceState({}, document.title, redirectUri); - await logtoClient.handleSignInCallback(exactRedirectUri); - console.log('TSCUT Auth: Login OK! Redirecionando...'); - window.location.assign('/'); + style.remove(); } catch (error) { style.remove(); - const logtoState = localStorage.getItem(`logto:${import.meta.env.VITE_LOGTO_APP_ID}:state`); - document.body.innerHTML = `
-

Falha na Identificação Industrial

-

- Erro: ${error.message} -

-
- Debug Info:
- - AppID: ${import.meta.env.VITE_LOGTO_APP_ID}
- - URI Usada: ${exactRedirectUri}
- - State na URL: ${urlParams.get('state')?.substring(0, 10)}...
- - State no Storage: ${logtoState ? 'Presente' : 'AUSENTE'}
-
+

Falha na Autenticação

+

Erro: ${error.message}

`; - console.error('Falha crítica no callback:', error); + console.error('Falha no callback do Logto:', error); } return; } @@ -73,16 +48,16 @@ async function protectPage() { try { const isAuthenticated = await logtoClient.isAuthenticated(); if (!isAuthenticated) { - console.log('TSCUT Auth: Iniciando fluxo de login...'); - await logtoClient.signIn(exactRedirectUri); + console.log('TSCUT Auth: Não autenticado. Redirecionando...'); + await logtoClient.signIn(redirectUri); return; } + // Acesso permitido style.remove(); - console.log('TSCUT: Acesso autorizado.'); } catch (err) { - console.error('Erro na verificação de sessão:', err); - await logtoClient.signIn(exactRedirectUri); + console.error('Erro de autenticação:', err); + await logtoClient.signIn(redirectUri); } }