TofuStack/src/routes/(auth)/register/+page.server.ts

30 lines
1.3 KiB
TypeScript
Raw Normal View History

2024-06-25 02:45:00 +00:00
import { fail, redirect } from '@sveltejs/kit';
import { zod } from 'sveltekit-superforms/adapters';
import { StatusCodes } from '$lib/constants/status-codes';
2024-08-31 18:35:16 +00:00
import { setError, superValidate } from 'sveltekit-superforms';
2024-09-01 21:32:50 +00:00
import { registerFormSchema, signInFormSchema } from './schemas';
2024-06-25 02:45:00 +00:00
export const load = async () => {
return {
2024-09-01 21:32:50 +00:00
emailRegisterForm: await superValidate(zod(registerFormSchema)),
emailSigninForm: await superValidate(zod(signInFormSchema))
2024-06-25 02:45:00 +00:00
};
};
export const actions = {
register: async ({ locals, request }) => {
2024-09-01 21:32:50 +00:00
const emailRegisterForm = await superValidate(request, zod(registerFormSchema));
if (!emailRegisterForm.valid) return fail(StatusCodes.BAD_REQUEST, { emailRegisterForm });
const { error } = await locals.api.iam.login.request.$post({ json: emailRegisterForm.data }).then(locals.parseApiResponse);
2024-06-25 02:45:00 +00:00
if (error) return setError(emailRegisterForm, 'email', error);
return { emailRegisterForm };
},
signin: async ({ locals, request }) => {
2024-09-01 21:32:50 +00:00
const emailSignInForm = await superValidate(request, zod(signInFormSchema));
if (!emailSignInForm.valid) return fail(StatusCodes.BAD_REQUEST, { emailSignInForm });
const { error } = await locals.api.iam.login.verify.$post({ json: emailSignInForm.data }).then(locals.parseApiResponse)
2024-06-25 02:45:00 +00:00
if (error) return setError(emailSignInForm, 'token', error);
redirect(301, '/');
}
};