added logout functionality

This commit is contained in:
Jonas_Jones 2025-03-07 19:14:05 +01:00
parent 9a2d157d37
commit e032477540
3 changed files with 20 additions and 0 deletions

View file

@ -12,6 +12,11 @@ export function redirectToLogin() {
window.location.href = `/login?returnUrl=${currentPath}`; window.location.href = `/login?returnUrl=${currentPath}`;
} }
export function resetSession() {
document.cookie = `sessionKey=; path=/; max-age=0`;
window.location.href = '/login';
}
export function ensureAuthenticated() { export function ensureAuthenticated() {
if (!getSessionKey()) { if (!getSessionKey()) {
redirectToLogin(); redirectToLogin();

View file

@ -29,6 +29,9 @@
<li aria-current={page.url.pathname === '/about' ? 'page' : undefined}> <li aria-current={page.url.pathname === '/about' ? 'page' : undefined}>
<a href="/about">About</a> <a href="/about">About</a>
</li> </li>
<li aria-current={page.url.pathname === '/logout' ? 'page' : undefined}>
<a href="/logout">Logout</a>
</li>
{/if} {/if}
</ul> </ul>
<svg viewBox="0 0 2 3" aria-hidden="true"> <svg viewBox="0 0 2 3" aria-hidden="true">

View file

@ -0,0 +1,12 @@
<script>
import { goto } from '$app/navigation';
import { resetSession } from '$lib/session.js';
resetSession();
goto('/login');
</script>
<div>
<h1>Logging out...</h1>
<p>Redirecting to the login page...</p>
</div>