/
This commit is contained in:
28
src/routes/(forms)/register/+page.server.ts
Normal file
28
src/routes/(forms)/register/+page.server.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
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 };
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user