Merge pull request 'fix/login-redirect-URL' (#439) from fix/login-redirect-URL into development

Reviewed-on: ADUANASOFT/anexo76#439
This commit is contained in:
2026-05-22 14:24:02 +00:00
8 changed files with 176 additions and 17 deletions

View File

@@ -50,8 +50,49 @@ export interface AuthState {
// Configuración de Keycloak
// ─────────────────────────────────────────────────────────
/**
* Devuelve la URL pública de Keycloak correcta para el browser.
* Si VITE_KEYCLOAK_URL está bakeado con localhost/127.0.0.1 pero el browser
* no está en localhost (producción), se ignora el valor bakeado y se deriva
* del hostname real del browser. Protege contra builds con .env de dev en prod.
*/
function resolveKeycloakUrl(): string {
const configured = (import.meta.env.VITE_KEYCLOAK_URL || '').replace(/\/+$/, '');
if (typeof window === 'undefined') {
// SSR: usar el valor configurado tal cual (el server tiene las vars correctas)
return configured || 'http://localhost:8085/kcauth';
}
const browserHostname = window.location.hostname;
const isLocalBrowser = browserHostname === 'localhost' || browserHostname === '127.0.0.1';
if (configured) {
try {
const parsed = new URL(configured);
const configuredHost = parsed.hostname;
const isLocalConfigured = configuredHost === 'localhost' || configuredHost === '127.0.0.1';
// Si el build fue con localhost pero el browser NO está en localhost → derivar del hostname real
if (isLocalConfigured && !isLocalBrowser) {
const protocol = window.location.protocol === 'https:' ? 'https:' : 'http:';
return `${protocol}//${browserHostname}/kcauth`;
}
} catch {
// URL malformada — caer al fallback
}
return configured;
}
if (isLocalBrowser) {
return 'http://localhost:8085/kcauth';
}
const protocol = window.location.protocol === 'https:' ? 'https:' : 'http:';
return `${protocol}//${browserHostname}/kcauth`;
}
const keycloakConfig = {
url: import.meta.env.VITE_KEYCLOAK_URL,
url: resolveKeycloakUrl(),
realm: import.meta.env.VITE_KEYCLOAK_REALM,
clientId: import.meta.env.VITE_KEYCLOAK_CLIENT_ID
};