diff --git a/src/app.d.ts b/src/app.d.ts index 276009c..b1bfd97 100644 --- a/src/app.d.ts +++ b/src/app.d.ts @@ -5,7 +5,10 @@ import type { User } from '$lib/types'; // for information about these interfaces declare global { namespace App { - // interface Error {} + interface Error { + message: string; + errorId: string; + } interface Locals { user: User | null; } @@ -16,4 +19,5 @@ declare global { } } -export {}; +export { }; + diff --git a/src/hooks.server.ts b/src/hooks.server.ts index 8e87a4b..8814496 100644 --- a/src/hooks.server.ts +++ b/src/hooks.server.ts @@ -1,4 +1,4 @@ -import type { Handle } from '@sveltejs/kit'; +import type { Handle, HandleFetch, HandleServerError } from '@sveltejs/kit'; import { API_URL } from '$env/static/private'; @@ -30,3 +30,32 @@ export const handle: Handle = async ({ event, resolve }) => { return resolve(event); }; + +export const handleFetch: HandleFetch = async ({ request, fetch, event: { cookies } }) => { + + const session = cookies.get('session'); + + if (!session) { + return fetch(request); + } + + request = new Request(request, { + headers: { + ...request.headers, + Authorization: `Bearer ${session}` + }, + }); + + return fetch(request); +} + +export const handleError: HandleServerError = async ({ error, status }) => { + const errorId = crypto.randomUUID(); + + console.error(`Error ID: ${errorId} - Status: ${status} - ${error}`); + + return { + message: 'Whoops!', + errorId, + }; +}; diff --git a/src/lib/types/database.ts b/src/lib/types/database.ts index d90eeb3..479e191 100644 --- a/src/lib/types/database.ts +++ b/src/lib/types/database.ts @@ -29,6 +29,7 @@ export interface Completion { export interface Group { id: number; name: string; + playerCount: number; chapter?: number; puzzle?: number; } diff --git a/src/lib/utils.ts b/src/lib/utils.ts index 230a1fb..210168d 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -1,12 +1,17 @@ -import { type ClassValue, clsx } from "clsx"; -import { twMerge } from "tailwind-merge"; +import { clsx, type ClassValue } from "clsx"; import { cubicOut } from "svelte/easing"; import type { TransitionConfig } from "svelte/transition"; +import { twMerge } from "tailwind-merge"; export function cn(...inputs: ClassValue[]) { return twMerge(clsx(inputs)); } +export const handleRedirect = (path: string, url: URL) => { + const redirectTo = url.pathname + url.search; + return `/${path}?redirectTo=${encodeURIComponent(redirectTo)}`; +} + type FlyAndScaleParams = { y?: number; x?: number; @@ -59,4 +64,4 @@ export const flyAndScale = ( }, easing: cubicOut }; -}; \ No newline at end of file +}; diff --git a/src/routes/(app)/+layout.server.ts b/src/routes/(app)/+layout.server.ts deleted file mode 100644 index b47fd2c..0000000 --- a/src/routes/(app)/+layout.server.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { redirect, type ServerLoad } from '@sveltejs/kit'; - -export const load: ServerLoad = async ({ parent }) => { - const { user } = await parent(); - if (!user) redirect(302, '/login'); -}; diff --git a/src/routes/(app)/+layout.svelte b/src/routes/(app)/+layout.svelte index f7c759e..686bac7 100644 --- a/src/routes/(app)/+layout.svelte +++ b/src/routes/(app)/+layout.svelte @@ -2,7 +2,6 @@ import { navigating } from '$app/stores'; import { Loader, Navbar, Sidenav } from '$lib/components/layout'; - import { Toaster } from '$lib/components/ui/sonner'; {#if $navigating} @@ -18,7 +17,6 @@ class="flex w-full flex-1 transform flex-col overflow-y-auto p-4 duration-300 ease-in-out" > - diff --git a/src/routes/(app)/+page.server.ts b/src/routes/(app)/+page.server.ts index aeaef58..5836b73 100644 --- a/src/routes/(app)/+page.server.ts +++ b/src/routes/(app)/+page.server.ts @@ -4,18 +4,13 @@ import { redirect } from '@sveltejs/kit'; import type { PageServerLoad } from './$types'; import type { Chapter } from '$lib/types'; +import { handleRedirect } from '$lib/utils'; -export const load = (async ({ fetch, cookies, locals: { user } }) => { +export const load = (async ({ url, fetch, locals: { user } }) => { - if (!user) redirect(302, '/login'); + if (!user) redirect(302, handleRedirect('login', url)); - const session = cookies.get('session'); - - const res = await fetch(`${API_URL}/chapters`, { - headers: { - Authorization: `Bearer ${session}` - } - }); + const res = await fetch(`${API_URL}/chapters`); if (!res.ok) { return { diff --git a/src/routes/(app)/badges/+page.server.ts b/src/routes/(app)/badges/+page.server.ts index d40dde6..00f3794 100644 --- a/src/routes/(app)/badges/+page.server.ts +++ b/src/routes/(app)/badges/+page.server.ts @@ -1,6 +1,8 @@ import { redirect } from '@sveltejs/kit'; import type { PageServerLoad } from './$types'; -export const load = (async ({ locals: { user } }) => { - if (!user) redirect(302, '/login'); -}) satisfies PageServerLoad; +import { handleRedirect } from '$lib/utils'; + +export const load: PageServerLoad = async ({ url, locals: { user } }) => { + if (!user) redirect(302, handleRedirect('login', url)); +} diff --git a/src/routes/(app)/chapters/+page.server.ts b/src/routes/(app)/chapters/+page.server.ts index d28906e..306f8fb 100644 --- a/src/routes/(app)/chapters/+page.server.ts +++ b/src/routes/(app)/chapters/+page.server.ts @@ -1,21 +1,16 @@ import { API_URL } from '$env/static/private'; +import { redirect } from '@sveltejs/kit'; import type { PageServerLoad } from './$types'; import type { Chapter } from '$lib/types'; -import { redirect } from '@sveltejs/kit'; +import { handleRedirect } from '$lib/utils'; -export const load = (async ({ locals: { user }, fetch, cookies }) => { +export const load = (async ({ url, locals: { user }, fetch }) => { - if (!user) redirect(302, '/login'); + if (!user) redirect(302, handleRedirect('login', url)); - const session = cookies.get('session'); - - const res = await fetch(`${API_URL}/chapters`, { - headers: { - Authorization: `Bearer ${session}` - } - }); + const res = await fetch(`${API_URL}/chapters`); if (!res.ok) { return { diff --git a/src/routes/(app)/chapters/[chapterId]/+page.server.ts b/src/routes/(app)/chapters/[chapterId]/+page.server.ts index 02a9cbc..f43be00 100644 --- a/src/routes/(app)/chapters/[chapterId]/+page.server.ts +++ b/src/routes/(app)/chapters/[chapterId]/+page.server.ts @@ -3,19 +3,14 @@ import { API_URL } from '$env/static/private'; import type { PageServerLoad } from './$types'; import type { Chapter } from '$lib/types'; +import { handleRedirect } from '$lib/utils'; import { redirect } from '@sveltejs/kit'; -export const load = (async ({ locals: { user }, fetch, cookies, params: { chapterId } }) => { +export const load = (async ({ url, locals: { user }, fetch, params: { chapterId } }) => { - if (!user) redirect(302, '/login'); + if (!user) redirect(302, handleRedirect('login', url)); - const session = cookies.get('session'); - - const res = await fetch(`${API_URL}/chapter/${chapterId}`, { - headers: { - Authorization: `Bearer ${session}` - } - }); + const res = await fetch(`${API_URL}/chapter/${chapterId}`); if (!res.ok) { redirect(302, '/chapters'); diff --git a/src/routes/(app)/chapters/[chapterId]/groups/+page.server.ts b/src/routes/(app)/chapters/[chapterId]/groups/+page.server.ts index 9a2b524..801fef8 100644 --- a/src/routes/(app)/chapters/[chapterId]/groups/+page.server.ts +++ b/src/routes/(app)/chapters/[chapterId]/groups/+page.server.ts @@ -3,19 +3,14 @@ import { API_URL } from '$env/static/private'; import type { Actions, PageServerLoad } from './$types'; import type { Chapter, Group } from '$lib/types'; +import { handleRedirect } from '$lib/utils'; import { redirect } from '@sveltejs/kit'; -export const load = (async ({ locals: { user }, fetch, cookies, params: { chapterId } }) => { +export const load: PageServerLoad = async ({ url, locals: { user }, fetch, params: { chapterId } }) => { - if (!user) redirect(302, '/login'); + if (!user) redirect(302, handleRedirect('login', url)); - const session = cookies.get('session'); - - let res = await fetch(`${API_URL}/chapter/${chapterId}`, { - headers: { - Authorization: `Bearer ${session}` - } - }); + let res = await fetch(`${API_URL}/chapter/${chapterId}`); if (!res.ok) { redirect(302, '/chapters'); @@ -27,12 +22,7 @@ export const load = (async ({ locals: { user }, fetch, cookies, params: { chapte redirect(302, '/chapters'); } - res = await fetch(`${API_URL}/groups/${chapter.id}`, { - headers: - { - Authorization: `Bearer ${session}` - } - }); + res = await fetch(`${API_URL}/groups/${chapter.id}`); if (!res.ok) { redirect(302, `/chapters/${chapterId}`); @@ -45,23 +35,18 @@ export const load = (async ({ locals: { user }, fetch, cookies, params: { chapte chapter, groups }; -}) satisfies PageServerLoad; +}; export const actions: Actions = { - join: async ({ fetch, params: { chapterId }, cookies, request }) => { + join: async ({ fetch, params: { chapterId }, request }) => { const data = await request.formData(); const name = data.get('name') as string; - const session = cookies.get('session'); - const res = await fetch(`${API_URL}/groupJoin`, { method: 'POST', - headers: { - Authorization: `Bearer ${session}` - }, body: JSON.stringify({ name, chapter: parseInt(chapterId), @@ -78,7 +63,7 @@ export const actions: Actions = { if (res.status === 403) { return { success: false, - message: 'Vous êtes déjà dans un groupe' + message: 'Vous êtes déjà dans un groupe ou le groupe est complet' }; } @@ -94,19 +79,14 @@ export const actions: Actions = { message: "Une erreur s'est produite" }; }, - leave: async ({ fetch, params: { chapterId }, cookies, request }) => { + leave: async ({ fetch, params: { chapterId }, request }) => { const data = await request.formData(); const name = data.get('name') as string; - const session = cookies.get('session'); - const res = await fetch(`${API_URL}/groupQuit`, { method: 'POST', - headers: { - Authorization: `Bearer ${session}` - }, body: JSON.stringify({ name, chapter: parseInt(chapterId), diff --git a/src/routes/(app)/chapters/[chapterId]/groups/+page.svelte b/src/routes/(app)/chapters/[chapterId]/groups/+page.svelte index 1ee5375..0277a9e 100644 --- a/src/routes/(app)/chapters/[chapterId]/groups/+page.svelte +++ b/src/routes/(app)/chapters/[chapterId]/groups/+page.svelte @@ -17,6 +17,8 @@ export let data: PageData; export let form: ActionData; + let limit = 4; + let name = ''; let submitting = false; @@ -58,19 +60,26 @@