Skip to content

Commit 5d8dd96

Browse files
committed
Fix redirects
1 parent 8be93be commit 5d8dd96

2 files changed

Lines changed: 25 additions & 2 deletions

File tree

src/routes/(auth)/login/+page.server.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,26 @@
1+
import { superValidate } from 'sveltekit-superforms';
2+
import { valibot } from 'sveltekit-superforms/adapters';
3+
import * as v from 'valibot';
14
import type { Actions, PageServerLoad } from './$types';
25
import { returnTo, tryVerifyCookie } from '$lib/server/auth';
36
import { QueueConnected } from '$lib/server/bullmq';
47

8+
const loginSchema = v.object({
9+
returnTo: v.optional(v.string())
10+
});
11+
512
export const load: PageServerLoad = async (event) => {
613
return {
7-
serviceAvailable: QueueConnected()
14+
serviceAvailable: QueueConnected(),
15+
form: await superValidate(valibot(loginSchema))
816
};
917
};
1018
export const actions: Actions = {
1119
async login(event) {
20+
const form = await superValidate(event.request, valibot(loginSchema));
21+
if (form.valid && form.data.returnTo) {
22+
event.url.searchParams.set('returnTo', form.data.returnTo);
23+
}
1224
await tryVerifyCookie(event, false);
1325
throw returnTo(event);
1426
}

src/routes/(auth)/login/+page.svelte

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<script lang="ts">
22
import { onDestroy } from 'svelte';
3+
import { superForm } from 'sveltekit-superforms';
34
import type { PageData } from './$types';
45
import { browser } from '$app/environment';
6+
import { page } from '$app/state';
57
import { env } from '$env/dynamic/public';
68
import ScriptoriaIcon from '$lib/icons/ScriptoriaIcon.svelte';
79
@@ -29,6 +31,15 @@
2931
timeout = null;
3032
}
3133
});
34+
35+
const { enhance } = superForm(data.form, {
36+
onSubmit(event) {
37+
const returnTo = page.url.searchParams.get('returnTo');
38+
if (returnTo) {
39+
event.formData.set('returnTo', returnTo);
40+
}
41+
}
42+
});
3243
</script>
3344

3445
<div class="card shadow-xl bg-white border p-4">
@@ -42,7 +53,7 @@
4253
BuildEngine serves as an interface between Scriptoria and the AWS resources it uses.
4354
</p>
4455
{#if data.serviceAvailable}
45-
<form method="POST" action="?/login" class="w-full">
56+
<form method="POST" action="?/login" class="w-full" use:enhance>
4657
<button class="btn btn-primary w-full">Login with Scriptoria</button>
4758
</form>
4859
{:else}

0 commit comments

Comments
 (0)