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:
@@ -1,6 +1,5 @@
|
||||
<script lang="ts">
|
||||
import { resolve } from '$app/paths';
|
||||
import { goto } from '$app/navigation';
|
||||
import { Button } from '$lib/components/ui/button/index.js';
|
||||
import { Input } from '$lib/components/ui/input/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 RequiredLabel from '$lib/components/layout/RequiredLabel.svelte';
|
||||
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
|
||||
import { zxcvbn, zxcvbnOptions } from '@zxcvbn-ts/core';
|
||||
|
||||
@@ -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 {};
|
||||
};
|
||||
Reference in New Issue
Block a user