cleanup validation logic

This commit is contained in:
d34dscene
2025-07-02 01:44:51 +02:00
parent 405d3947dd
commit ba2ec6db3d
65 changed files with 8571 additions and 9434 deletions

View File

@@ -1,51 +1,34 @@
import type { LayoutLoad } from './$types';
import { goto } from '$app/navigation';
import { useClient } from '$lib/api';
import { profile } from '$lib/stores/profile';
import { user } from '$lib/stores/user';
import { UserService } from '$lib/gen/mantrae/v1/user_pb';
import { ProfileService } from '$lib/gen/mantrae/v1/profile_pb';
import type { LayoutLoad } from "./$types";
import { goto } from "$app/navigation";
import { useClient } from "$lib/api";
import { profile } from "$lib/stores/profile";
import { user } from "$lib/stores/user";
import { UserService } from "$lib/gen/mantrae/v1/user_pb";
import { ProfileService } from "$lib/gen/mantrae/v1/profile_pb";
export const ssr = false;
export const prerender = false;
export const trailingSlash = 'always';
const isPublicRoute = (path: string) => {
return path.startsWith('/login') || path === '/login';
};
export const prerender = true;
export const trailingSlash = "always";
export const load: LayoutLoad = async ({ url, fetch }) => {
const currentPath = url.pathname;
const isPublic = isPublicRoute(currentPath);
// Check if cookie is set
const isPublic = currentPath.startsWith("/login");
try {
const userClient = useClient(UserService, fetch);
const resUser = await userClient.verifyJWT({});
const resUser = await userClient.getUser({});
if (!resUser.user) throw new Error("Authentication failed");
user.value = resUser.user;
if (resUser.user) {
user.value = resUser.user;
// Update profile if not set
if (!profile.id) {
const profileClient = useClient(ProfileService, fetch);
const resProfile = await profileClient.listProfiles({});
profile.value = resProfile.profiles[0];
}
if (isPublic) {
// Authenticated user trying to access login page - redirect to home
await goto('/');
return;
}
return;
} else {
throw new Error('Authentication failed');
if (!profile.id) {
const profileClient = useClient(ProfileService, fetch);
const resProfile = await profileClient.listProfiles({});
profile.value = resProfile.profiles[0];
}
if (isPublic) await goto("/");
} catch (_) {
user.clear();
if (!isPublic) {
await goto('/login');
}
return;
if (!isPublic) await goto("/login");
}
};