feat: Implement dynamic backend URL configuration and streamline logout process by centralizing audit logging.
This commit is contained in:
@@ -367,10 +367,9 @@ export const logout = async () => {
|
||||
if (!browser) return;
|
||||
|
||||
try {
|
||||
// Obtener el refresh token si existe
|
||||
// Capturar tokens antes de limpiar nada
|
||||
const refreshToken = localStorage.getItem('refresh_token');
|
||||
|
||||
|
||||
const accessToken = localStorage.getItem('access_token');
|
||||
|
||||
// Limpiar store de compañías
|
||||
try {
|
||||
@@ -389,6 +388,15 @@ export const logout = async () => {
|
||||
|
||||
// Si hay instancia de Keycloak, hacer logout de Keycloak
|
||||
if (keycloakInstance?.authenticated) {
|
||||
// Primero notificamos al servidor para limpieza de cookies (SvelteKit)
|
||||
try {
|
||||
await fetch('/logout', {
|
||||
method: 'POST'
|
||||
});
|
||||
} catch (e) {
|
||||
console.error("Error calling server logout:", e);
|
||||
}
|
||||
|
||||
await keycloakInstance.logout({
|
||||
redirectUri: window.location.origin + '/login'
|
||||
});
|
||||
@@ -400,6 +408,9 @@ export const logout = async () => {
|
||||
const form = document.createElement('form');
|
||||
form.method = 'POST';
|
||||
form.action = '/logout';
|
||||
|
||||
|
||||
|
||||
document.body.appendChild(form);
|
||||
form.submit();
|
||||
|
||||
|
||||
@@ -1,59 +1,48 @@
|
||||
<script lang="ts">
|
||||
import { onMount } from 'svelte';
|
||||
import { companyStore } from '$lib/stores/company.svelte';
|
||||
import { getExchangeRates } from '$lib/api/dashboard/a76/general_catalogs/exchange-rate';
|
||||
import CreateEditDialog from './create-edit-dialog.svelte';
|
||||
import { onMount } from 'svelte';
|
||||
import { companyStore } from '$lib/stores/company.svelte';
|
||||
import { getExchangeRates } from '$lib/api/dashboard/a76/general_catalogs/exchange-rate';
|
||||
import CreateEditDialog from './create-edit-dialog.svelte';
|
||||
|
||||
let open = $state(false);
|
||||
let checked = $state(false);
|
||||
let open = $state(false);
|
||||
let checked = $state(false);
|
||||
|
||||
async function checkExchangeRate() {
|
||||
console.log('[ExchangeRateGuard] Checking...', companyStore.activeCompany);
|
||||
if (!companyStore.activeCompany?.id) {
|
||||
console.log('[ExchangeRateGuard] No active company');
|
||||
return;
|
||||
}
|
||||
async function checkExchangeRate() {
|
||||
if (!companyStore.activeCompany?.id) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Use local date instead of UTC
|
||||
const today = new Date().toLocaleDateString('fr-CA'); // YYYY-MM-DD
|
||||
console.log('[ExchangeRateGuard] Date:', today);
|
||||
|
||||
try {
|
||||
const response = await getExchangeRates(companyStore.activeCompany.id, {
|
||||
date: today,
|
||||
page_size: 1
|
||||
});
|
||||
console.log('[ExchangeRateGuard] Response (stringified):', JSON.stringify(response, null, 2));
|
||||
// Use local date instead of UTC
|
||||
const today = new Date().toLocaleDateString('fr-CA'); // YYYY-MM-DD
|
||||
|
||||
// api.get returns { data: ..., status: ... } and types now reflect that
|
||||
const items = response.data?.items || [];
|
||||
|
||||
if (items.length === 0) {
|
||||
console.log('[ExchangeRateGuard] No rate found, opening modal');
|
||||
open = true;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('[ExchangeRateGuard] Error checking exchange rate:', error);
|
||||
} finally {
|
||||
checked = true;
|
||||
}
|
||||
}
|
||||
try {
|
||||
const response = await getExchangeRates(companyStore.activeCompany.id, {
|
||||
date: today,
|
||||
page_size: 1
|
||||
});
|
||||
|
||||
$effect(() => {
|
||||
if (companyStore.activeCompany?.id && !checked) {
|
||||
checkExchangeRate();
|
||||
}
|
||||
});
|
||||
// api.get returns { data: ..., status: ... } and types now reflect that
|
||||
const items = response.data?.items || [];
|
||||
|
||||
function handleSuccess() {
|
||||
|
||||
console.log('Exchange rate created successfully via guard');
|
||||
checked = true;
|
||||
}
|
||||
if (items.length === 0) {
|
||||
open = true;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('[ExchangeRateGuard] Error checking exchange rate:', error);
|
||||
} finally {
|
||||
checked = true;
|
||||
}
|
||||
}
|
||||
|
||||
$effect(() => {
|
||||
if (companyStore.activeCompany?.id && !checked) {
|
||||
checkExchangeRate();
|
||||
}
|
||||
});
|
||||
|
||||
function handleSuccess() {
|
||||
checked = true;
|
||||
}
|
||||
</script>
|
||||
|
||||
<CreateEditDialog
|
||||
bind:open
|
||||
onSuccess={handleSuccess}
|
||||
overlayClass="bg-black/20"
|
||||
/>
|
||||
<CreateEditDialog bind:open onSuccess={handleSuccess} overlayClass="bg-black/20" />
|
||||
|
||||
51
frontend/src/lib/config/backend.ts
Normal file
51
frontend/src/lib/config/backend.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
/**
|
||||
* Configuración de conexión al backend
|
||||
* Detecta automáticamente el entorno y usa la URL correcta
|
||||
*/
|
||||
|
||||
export function getBackendUrl(): string {
|
||||
// 1. Si existe variable de entorno, úsala (override manual)
|
||||
if (import.meta.env.VITE_BACKEND_URL) {
|
||||
return import.meta.env.VITE_BACKEND_URL;
|
||||
}
|
||||
|
||||
// 2. Detección automática basada en dónde corre el código
|
||||
if (typeof window !== 'undefined') {
|
||||
// CLIENTE (Browser): usar la URL pública del backend
|
||||
// En local: http://localhost:8000
|
||||
// En prod: mismo dominio o dominio específico
|
||||
const hostname = window.location.hostname;
|
||||
|
||||
if (hostname === 'localhost' || hostname === '127.0.0.1') {
|
||||
return 'http://localhost:8000/api';
|
||||
}
|
||||
|
||||
// En producción, asumir que el backend está en el mismo dominio /api
|
||||
// o usar un subdominio específico
|
||||
return `${window.location.protocol}//${hostname}/api`;
|
||||
} else {
|
||||
// SERVIDOR (SvelteKit SSR/Endpoints): usar URL interna
|
||||
// En Docker: http://backend:8000
|
||||
// En local: http://127.0.0.1:8000 (IPv4 explícito)
|
||||
|
||||
// Detectar si estamos en Docker por hostname
|
||||
const isDocker = process.env.HOSTNAME?.includes('docker');
|
||||
|
||||
if (isDocker) {
|
||||
return 'http://backend:8000/api';
|
||||
}
|
||||
|
||||
// En desarrollo local, usar IPv4 explícito para evitar problemas con IPv6
|
||||
return 'http://127.0.0.1:8000/api';
|
||||
}
|
||||
}
|
||||
|
||||
export const BACKEND_URL = getBackendUrl();
|
||||
|
||||
// Helper para logs
|
||||
export function logBackendConfig() {
|
||||
console.log('[Backend Config]', {
|
||||
url: BACKEND_URL,
|
||||
isServer: typeof window === 'undefined',
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user