Files
wotra/web/src/routes/+layout.svelte
T
aksdb d750b8c3c9 🐛 Recover the app when the session expires
A 401 mid-session made the PocketBase SDK drop the auth store, while
session.userId and the gate of the layout stayed set: the user remained
in the app, every round failed, /login was bounced back and a token-less
pull was even taken for a successful round.

The sync layer now ends the session through an onSessionExpired hook
when a round fails with 401 or finds the auth store cleared, and reports
no round as synced without a valid auth store. The session module tries
one token refresh before it clears the session, and the layout resolves
the gate again whenever the auth store is no longer valid, so the user
lands on the login page and the next sign-in brings the data back.
2026-12-01 19:04:06 +01:00

208 lines
4.6 KiB
Svelte

<script lang="ts">
import { onMount } from 'svelte';
import { goto } from '$app/navigation';
import { page } from '$app/state';
import { pendingCount, status, syncNow } from '$lib/db.svelte';
import { gateStale, resolveGate, safeNext, session } from '$lib/session.svelte';
let { children } = $props();
let gate = $state<'login' | 'app' | null>(null);
async function route() {
if (gateStale(gate)) gate = await resolveGate();
const path = page.url.pathname;
if (gate === 'app') {
if (path === '/' || path === '/login') await goto(safeNext(page.url), { replaceState: true });
} else if (gate === 'login' && path !== '/login') {
await goto(
'/login' + (path === '/' ? '' : '?next=' + encodeURIComponent(path + page.url.search)),
{ replaceState: true }
);
}
}
$effect(() => {
page.url.pathname;
session.userId;
if (gate !== null) void route();
});
// Tick every 15s so "Xs ago" stays fresh.
let tick = $state(0);
onMount(() => {
void resolveGate().then((g) => (gate = g));
if (import.meta.env.PROD && 'serviceWorker' in navigator) {
navigator.serviceWorker.register('/sw.js', { scope: '/' }).catch(() => {});
}
const tickInterval = setInterval(() => tick++, 15_000);
return () => clearInterval(tickInterval);
});
const inApp = $derived(gate === 'app' && status.ready);
const todayActive = $derived(page.url.pathname === '/today');
const weekActive = $derived(
page.url.pathname === '/week' && !todayActive
);
const pending = $derived(status.ready ? pendingCount() : 0);
const error = $derived(status.error);
const online = $derived(status.online);
const lastSync = $derived(status.lastSync);
function formatLastSynced(ms: number): string {
if (!ms) return 'never';
const diff = Math.floor((Date.now() - ms) / 1000);
if (diff < 5) return 'just now';
if (diff < 60) return `${diff}s ago`;
if (diff < 3600) return `${Math.floor(diff / 60)}m ago`;
return new Date(ms).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
}
const syncTitle = $derived.by(() => {
tick;
return error ?? (!online ? 'Offline' : `Last synced: ${formatLastSynced(lastSync)}`);
});
</script>
{#if inApp}
<nav>
<div class="nav-links">
<a href="/today" class:active={todayActive}>Today</a>
<a href="/week" class:active={weekActive}>Week</a>
<a href="/history" class:active={page.url.pathname === '/history'}>History</a>
<a href="/settings" class:active={page.url.pathname === '/settings'}>Settings</a>
</div>
<button
class="sync-btn"
class:error={error !== null}
onclick={() => void syncNow()}
title={syncTitle}
aria-label="Sync"
>
{#if error}
<span>⚠</span>
{:else}
<span>↻</span>
{/if}
{#if pending > 0}
<span class="badge">{pending}</span>
{/if}
</button>
</nav>
<main>
{@render children()}
</main>
{:else if gate === 'login'}
<div class="auth-wrap">
{@render children()}
</div>
{:else}
<p class="loading">Loading…</p>
{/if}
<style>
:global(*, *::before, *::after) {
box-sizing: border-box;
}
:global(body) {
margin: 0;
font-family: system-ui, sans-serif;
background: #f8f9fa;
color: #212529;
}
:global(button) {
cursor: pointer;
}
nav {
display: flex;
align-items: center;
justify-content: space-between;
padding: 0.75rem 1rem;
background: #1a1a2e;
position: sticky;
top: 0;
z-index: 10;
}
.nav-links {
display: flex;
gap: 0.5rem;
}
nav a {
color: #adb5bd;
text-decoration: none;
padding: 0.4rem 0.8rem;
border-radius: 6px;
font-size: 0.9rem;
font-weight: 500;
transition: background 0.15s, color 0.15s;
}
nav a:hover {
background: #16213e;
color: #e9ecef;
}
nav a.active {
background: #0f3460;
color: #e9ecef;
}
.sync-btn {
position: relative;
display: flex;
align-items: center;
gap: 0.3rem;
background: none;
border: 1px solid #2e2e4e;
border-radius: 6px;
color: #adb5bd;
font-size: 1rem;
padding: 0.3rem 0.6rem;
line-height: 1;
transition: color 0.15s, border-color 0.15s;
cursor: pointer;
}
.sync-btn:hover {
color: #e9ecef;
border-color: #555580;
}
.sync-btn.error {
color: #ff6b6b;
border-color: #ff6b6b55;
}
.badge {
font-size: 0.65rem;
font-weight: 700;
background: #e67e22;
color: #fff;
border-radius: 999px;
padding: 0.05rem 0.35rem;
line-height: 1.4;
}
main {
max-width: 800px;
margin: 0 auto;
padding: 1.5rem 1rem;
}
.auth-wrap {
min-height: 100dvh;
display: flex;
align-items: center;
justify-content: center;
padding: 1rem;
}
.loading {
min-height: 100dvh;
display: flex;
align-items: center;
justify-content: center;
margin: 0;
color: #6c757d;
}
</style>