Fix session cookie propagation; desktop creates session via POST before opening portal

This commit is contained in:
Esdras Renan 2025-10-14 20:33:40 -03:00
parent 69955ae80c
commit 6754af769b
3 changed files with 62 additions and 12 deletions

View file

@ -430,14 +430,36 @@ function App() {
}
}
const openSystem = useCallback(() => {
const openSystem = useCallback(async () => {
if (!token) return
setIsLaunchingSystem(true)
try {
// Tenta criar a sessão via API (evita dependência de redirecionamento + cookies em 3xx)
const res = await fetch(`${apiBaseUrl}/api/machines/sessions`, {
method: "POST",
credentials: "include",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ machineToken: token, rememberMe: true }),
})
if (!res.ok) {
// Fallback para o handshake por redirecionamento
const persona = (config?.accessRole ?? accessRole) === "manager" ? "manager" : "collaborator"
const redirectTarget = persona === "manager" ? "/dashboard" : "/portal"
const url = `${resolvedAppUrl}/machines/handshake?token=${encodeURIComponent(token)}&redirect=${encodeURIComponent(redirectTarget)}`
window.location.href = url
return
}
} catch {
const persona = (config?.accessRole ?? accessRole) === "manager" ? "manager" : "collaborator"
const redirectTarget = persona === "manager" ? "/dashboard" : "/portal"
const url = `${resolvedAppUrl}/machines/handshake?token=${encodeURIComponent(token)}&redirect=${encodeURIComponent(redirectTarget)}`
window.location.href = url
return
}
const persona = (config?.accessRole ?? accessRole) === "manager" ? "manager" : "collaborator"
const redirectTarget = persona === "manager" ? "/dashboard" : "/portal"
const url = `${resolvedAppUrl}/machines/handshake?token=${encodeURIComponent(token)}&redirect=${encodeURIComponent(redirectTarget)}`
window.location.href = url
}, [token, config?.accessRole, accessRole, resolvedAppUrl])
window.location.href = `${resolvedAppUrl}${redirectTarget}`
}, [token, config?.accessRole, accessRole, resolvedAppUrl, apiBaseUrl])
async function reprovision() {
if (!store) return