29 lines
968 B
TypeScript
29 lines
968 B
TypeScript
import type { PageServerLoad } from './$types';
|
|
import { superValidate } from 'sveltekit-superforms';
|
|
import { type Actions, fail } from '@sveltejs/kit';
|
|
import { zod } from 'sveltekit-superforms/adapters';
|
|
import { registerFormSchema } from './register-form.svelte';
|
|
import { registerUser } from '$lib/api/user';
|
|
import { isErrorResponse } from '$lib/types';
|
|
|
|
export const load = (async () => {
|
|
return { form: await superValidate(zod(registerFormSchema)) };
|
|
}) satisfies PageServerLoad;
|
|
|
|
export const actions: Actions = {
|
|
default: async (event) => {
|
|
const form = await superValidate(event, zod(registerFormSchema));
|
|
|
|
if (!form.valid) return fail(400, { form });
|
|
|
|
const response = await registerUser(form.data.username, form.data.password);
|
|
|
|
if (isErrorResponse(response)) {
|
|
form.errors.username = [response.error];
|
|
return fail(400, { form });
|
|
}
|
|
|
|
return { form, success: true };
|
|
}
|
|
};
|