Added redirectTo

This commit is contained in:
glazk0 2024-03-31 23:55:56 +02:00
parent c98b641bb1
commit 7e2d80f971
No known key found for this signature in database
GPG key ID: E45BF177782B9FEB
22 changed files with 84 additions and 62 deletions

View file

@ -29,6 +29,7 @@ export interface Completion {
export interface Group {
id: number;
name: string;
playerCount: number;
chapter?: number;
puzzle?: number;
}

View file

@ -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
};
};
};

View file

@ -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');
};

View file

@ -2,7 +2,6 @@
import { navigating } from '$app/stores';
import { Loader, Navbar, Sidenav } from '$lib/components/layout';
import { Toaster } from '$lib/components/ui/sonner';
</script>
{#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"
>
<slot />
<Toaster position="top-right" />
</div>
</div>
</div>

View file

@ -4,10 +4,11 @@ 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, cookies, locals: { user } }) => {
if (!user) redirect(302, '/login');
if (!user) redirect(302, handleRedirect('login', url));
const session = cookies.get('session');

View file

@ -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));
}

View file

@ -1,13 +1,14 @@
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, cookies }) => {
if (!user) redirect(302, '/login');
if (!user) redirect(302, handleRedirect('login', url));
const session = cookies.get('session');

View file

@ -3,11 +3,12 @@ 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, cookies, params: { chapterId } }) => {
if (!user) redirect(302, '/login');
if (!user) redirect(302, handleRedirect('login', url));
const session = cookies.get('session');

View file

@ -3,11 +3,12 @@ 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, cookies, params: { chapterId } }) => {
if (!user) redirect(302, '/login');
if (!user) redirect(302, handleRedirect('login', url));
const session = cookies.get('session');
@ -45,7 +46,7 @@ export const load = (async ({ locals: { user }, fetch, cookies, params: { chapte
chapter,
groups
};
}) satisfies PageServerLoad;
};
export const actions: Actions = {

View file

@ -17,6 +17,8 @@
export let data: PageData;
export let form: ActionData;
let limit = 4;
let name = '';
let submitting = false;
@ -58,19 +60,26 @@
</div>
<ul class="flex flex-col gap-2">
{#if filteredGroups.length === 0}
<li class="flex h-16 w-full items-center justify-center rounded border border-border bg-card">
<li
class="flex min-h-16 w-full items-center justify-center rounded border border-border bg-card"
>
<p class="text-muted-foreground">Aucun groupe trouvé</p>
</li>
{:else}
{#each filteredGroups as group (group.name)}
<li class="group relative flex h-full w-full flex-col rounded border border-border bg-card">
<div class="flex h-full w-full items-center justify-between gap-4 p-4">
<li
class="group relative flex min-h-16 w-full flex-col rounded border border-border bg-card"
>
<div class="flex items-center justify-between gap-4 p-4">
<div class="flex items-center gap-2">
<span class="font-semibold">
{group.name}
</span>
<span class="text-muted-foreground">
{group.playerCount} membres
</span>
</div>
<div>
{#if group.playerCount < limit}
{#if $page.data.user?.groups.some((g) => g.name === group.name)}
<form
method="post"
@ -127,7 +136,7 @@
</Button>
</form>
{/if}
</div>
{/if}
</div>
</li>
{/each}

View file

@ -1,15 +1,16 @@
import { API_URL } from "$env/static/private";
import { fail, redirect, type Actions } from "@sveltejs/kit";
import type { PageServerLoad } from "./$types";
import { superValidate } from "sveltekit-superforms";
import { zod } from "sveltekit-superforms/adapters";
import { API_URL } from "$env/static/private";
import { handleRedirect } from "$lib/utils";
import { groupSchema } from "$lib/validations/group";
export const load: PageServerLoad = async ({ params: { chapterId }, locals: { user } }) => {
export const load: PageServerLoad = async ({ url, params: { chapterId }, locals: { user } }) => {
if (!user) redirect(302, '/login');
if (!user) redirect(302, handleRedirect('login', url));
if (user.groups.find(g => g.chapter === parseInt(chapterId))) {
redirect(302, `/chapters/${chapterId}/groups`);
@ -24,9 +25,9 @@ export const load: PageServerLoad = async ({ params: { chapterId }, locals: { us
};
export const actions: Actions = {
default: async ({ locals: { user }, fetch, request, cookies, params: { chapterId } }) => {
default: async ({ url, locals: { user }, fetch, request, cookies, params: { chapterId } }) => {
if (!user) redirect(302, '/login');
if (!user) redirect(302, handleRedirect('login', url));
if (!chapterId) redirect(302, '/chapters');

View file

@ -1,12 +1,13 @@
import { API_URL } from '$env/static/private';
import type { PageServerLoad } from './$types';
import { redirect } from '@sveltejs/kit';
import type { PageServerLoad } from './$types';
import type { LeaderboardEvent } from '$lib/types';
import { handleRedirect } from '$lib/utils';
export const load: PageServerLoad = async ({ locals: { user }, fetch, cookies, params: { chapterId } }) => {
export const load: PageServerLoad = async ({ url, locals: { user }, fetch, cookies, params: { chapterId } }) => {
if (!user) redirect(302, '/login');
if (!user) redirect(302, handleRedirect('login', url));
if (!chapterId) redirect(302, '/');

View file

@ -2,7 +2,9 @@ import { redirect } from '@sveltejs/kit';
import type { PageServerLoad } from './$types';
export const load = (async ({ locals: { user }, params: { chapterId } }) => {
if (!user) redirect(302, '/login');
import { handleRedirect } from '$lib/utils';
export const load = (async ({ url, locals: { user }, params: { chapterId } }) => {
if (!user) redirect(302, handleRedirect('login', url));
redirect(302, chapterId ? `/chapters/${chapterId}` : `/chapters`);
}) satisfies PageServerLoad;

View file

@ -1,13 +1,14 @@
import { API_URL } from '$env/static/private';
import { error, redirect, type Actions } from '@sveltejs/kit';
import type { PageServerLoad } from './$types';
import { compile } from 'mdsvex';
import type { PageServerLoad } from './$types';
import type Puzzle from '$lib/components/puzzle.svelte';
import type { Chapter } from '$lib/types';
import { handleRedirect } from '$lib/utils';
export const load = (async ({ locals: { user }, fetch, cookies, params: { chapterId, puzzleId } }) => {
if (!user) redirect(302, '/login');
export const load = (async ({ url, locals: { user }, fetch, cookies, params: { chapterId, puzzleId } }) => {
if (!user) redirect(302, handleRedirect('login', url));
const session = cookies.get('session');

View file

@ -1,4 +0,0 @@
<script lang="ts">
</script>
TODO

View file

@ -4,10 +4,11 @@ import { redirect } from '@sveltejs/kit';
import type { PageServerLoad } from './$types';
import type { Leaderboard } from '$lib/types';
import { handleRedirect } from '$lib/utils';
export const load = (async ({ locals: { user }, fetch, cookies }) => {
export const load = (async ({ url, locals: { user }, fetch, cookies }) => {
if (!user) redirect(302, '/login');
if (!user) redirect(302, handleRedirect('login', url));
const session = cookies.get('session');

View file

@ -2,12 +2,14 @@ import { API_URL } from '$env/static/private';
import { fail, redirect, type Actions } from '@sveltejs/kit';
import type { PageServerLoad } from './$types';
import { settingSchema } from '$lib/validations/auth';
import { zod } from 'sveltekit-superforms/adapters';
import { setError, superValidate } from 'sveltekit-superforms/server';
export const load: PageServerLoad = async ({ locals: { user } }) => {
if (!user) redirect(302, '/login');
import { handleRedirect } from '$lib/utils';
import { settingSchema } from '$lib/validations/auth';
export const load: PageServerLoad = async ({ url, locals: { user } }) => {
if (!user) redirect(302, handleRedirect('login', url));
const form = await superValidate(user, zod(settingSchema));

View file

@ -1,7 +0,0 @@
<script lang="ts">
import { Toaster } from '$lib/components/ui/sonner';
</script>
<slot />
<Toaster />

View file

@ -21,7 +21,7 @@ export const load: PageServerLoad = async ({ locals: { user } }) => {
};
export const actions: Actions = {
default: async ({ request, cookies, fetch }) => {
default: async ({ request, cookies, fetch, url: { searchParams } }) => {
const form = await superValidate(request, zod(loginSchema));
@ -52,6 +52,11 @@ export const actions: Actions = {
sameSite: 'strict',
});
const redirectTo = searchParams.get('redirectTo');
if (redirectTo)
redirect(302, `/${redirectTo.slice(1)}`);
redirect(302, '/');
}
};

View file

@ -53,7 +53,7 @@ export const actions: Actions = {
form
};
},
confirmation: async ({ request, cookies }) => {
confirmation: async ({ request, cookies, url: { searchParams } }) => {
const form = await superValidate(request, zod(registerConfirmationSchema));
if (!form.valid) {
@ -93,6 +93,11 @@ export const actions: Actions = {
sameSite: 'strict',
});
const redirectTo = searchParams.get('redirectTo');
if (redirectTo)
redirect(302, `/${redirectTo.slice(1)}`);
redirect(302, '/');
},
}
}

View file

@ -11,7 +11,6 @@
import Input from '$lib/components/ui/input/input.svelte';
import { registerConfirmationSchema, registerSchema } from '$lib/validations/auth';
import { enhance } from '$app/forms';
export let data: PageData;

View file

@ -2,8 +2,11 @@
import '../app.css';
import { Metadata } from '$lib/components';
import { Toaster } from '$lib/components/ui/sonner';
</script>
<Metadata />
<slot />
<Toaster />