fix(login): redirect authenticated users before page render

Replaced -based redirect with +page.ts load function. Checks localStorage token directly and throws redirect(302) before the component mounts, preventing any flash of the login page.

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
2026-06-04 19:48:08 +01:00
co-authored by Sisyphus
parent f083da6345
commit ae1bc0f91a
2 changed files with 23 additions and 9 deletions
-9
View File
@@ -1,6 +1,5 @@
<script lang="ts"> <script lang="ts">
import { resolve } from '$app/paths'; import { resolve } from '$app/paths';
import { goto } from '$app/navigation';
import { Button } from '$lib/components/ui/button/index.js'; import { Button } from '$lib/components/ui/button/index.js';
import { Input } from '$lib/components/ui/input/index.js'; import { Input } from '$lib/components/ui/input/index.js';
import { Label } from '$lib/components/ui/label/index.js'; import { Label } from '$lib/components/ui/label/index.js';
@@ -8,14 +7,6 @@
import { Checkbox } from '$lib/components/ui/checkbox/index.js'; import { Checkbox } from '$lib/components/ui/checkbox/index.js';
import RequiredLabel from '$lib/components/layout/RequiredLabel.svelte'; import RequiredLabel from '$lib/components/layout/RequiredLabel.svelte';
import { SvelteDate } from 'svelte/reactivity'; import { SvelteDate } from 'svelte/reactivity';
import { authStore } from '$lib/stores/auth.svelte.js';
// Redirect authenticated users away from login page
$effect(() => {
if (authStore.isAuthenticated && authStore.hasLoaded) {
goto(authStore.isAdmin() ? resolve('/today') : resolve('/'));
}
});
// zxcvbn-ts imports // zxcvbn-ts imports
import { zxcvbn, zxcvbnOptions } from '@zxcvbn-ts/core'; import { zxcvbn, zxcvbnOptions } from '@zxcvbn-ts/core';
+23
View File
@@ -0,0 +1,23 @@
import { redirect } from '@sveltejs/kit';
import { resolve } from '$app/paths';
import type { PageLoad } from './$types';
export const load: PageLoad = async () => {
if (typeof window !== 'undefined') {
const token = localStorage.getItem('authToken');
if (token) {
try {
const payload = JSON.parse(atob(token.split('.')[1]));
if (payload.exp * 1000 > Date.now()) {
if (payload.role === 'admin') {
throw redirect(302, resolve('/today'));
}
throw redirect(302, resolve('/'));
}
} catch (e) {
if (e instanceof Error && e.name === 'RedirectError') throw e;
}
}
}
return {};
};