feat: cerrar sesión por POST con redirección a / y formulario en UI

Co-authored-by: aider (openrouter/openai/gpt-5) <aider@aider.chat>
webui
brobert 2 weeks ago
parent 5637c90d2d
commit d84fd9a772

@ -1,4 +1,5 @@
import type { RequestHandler } from './$types'; import type { RequestHandler } from './$types';
import { redirect } from '@sveltejs/kit';
import { getDb } from '$lib/server/db'; import { getDb } from '$lib/server/db';
import { sha256Hex } from '$lib/server/crypto'; import { sha256Hex } from '$lib/server/crypto';
import { isProd } from '$lib/server/env'; import { isProd } from '$lib/server/env';
@ -25,5 +26,7 @@ export const POST: RequestHandler = async (event) => {
} }
// Limpiar cookie (asegurar mismos atributos que al crearla) // Limpiar cookie (asegurar mismos atributos que al crearla)
event.cookies.delete('sid', { path: '/', httpOnly: true, sameSite: 'lax', secure: isProd() }); event.cookies.delete('sid', { path: '/', httpOnly: true, sameSite: 'lax', secure: isProd() });
return new Response(null, { status: 204 });
// Redirigir a home para que el navegador navegue sin depender de JS
throw redirect(303, '/');
}; };

@ -11,19 +11,15 @@
}>; }>;
}; };
async function logout() {
try {
await fetch('/api/logout', { method: 'POST' });
} catch {}
location.href = '/';
}
</script> </script>
<h1>Panel</h1> <h1>Panel</h1>
<p>Sesión iniciada como: <strong>{data.userId}</strong></p> <p>Sesión iniciada como: <strong>{data.userId}</strong></p>
<div style="margin: 1rem 0;"> <div style="margin: 1rem 0;">
<button on:click={logout}>Cerrar sesión</button> <form method="POST" action="/api/logout">
<button type="submit">Cerrar sesión</button>
</form>
</div> </div>
<h2>Mis tareas (abiertas)</h2> <h2>Mis tareas (abiertas)</h2>

Loading…
Cancel
Save