refactor: streamline authentication logic in dashboard and login routes by removing unnecessary error handling and improving user session validation, enhancing code clarity and maintainability

This commit is contained in:
2025-12-17 11:28:08 -03:00
parent 9072619e26
commit 551a2fed00
3 changed files with 21 additions and 29 deletions

View File

@@ -1,21 +1,17 @@
import { createConvexHttpClient } from '@mmailaender/convex-better-auth-svelte/sveltekit';
import { api } from '@sgse-app/backend/convex/_generated/api';
import { error, redirect } from '@sveltejs/kit';
import type { FunctionReference } from 'convex/server';
import { redirect } from '@sveltejs/kit';
export const load = async ({ locals, url }) => {
if (!locals.token) {
throw redirect(302, '/login?redirect=' + url.pathname);
}
try {
const client = createConvexHttpClient({ token: locals.token });
const currentUser = await client.query(api.auth.getCurrentUser as FunctionReference<'query'>);
if (!currentUser) {
throw redirect(302, '/login?redirect=' + url.pathname);
}
return { currentUser };
} catch {
return error(500, 'Ops! Ocorreu um erro, tente novamente mais tarde.');
const client = createConvexHttpClient({ token: locals.token });
const currentUser = await client.query(api.auth.getCurrentUser);
if (!currentUser) {
throw redirect(302, '/login?redirect=' + url.pathname);
}
return { currentUser };
};