2024-11-09 19:05:28 +00:00
|
|
|
import { signupUsernameEmailDto } from '$lib/dtos/signup-username-email.dto';
|
|
|
|
|
import { type Actions, fail } from '@sveltejs/kit';
|
|
|
|
|
import { redirect } from 'sveltekit-flash-message/server';
|
|
|
|
|
import { zod } from 'sveltekit-superforms/adapters';
|
|
|
|
|
import { setError, superValidate } from 'sveltekit-superforms/server';
|
|
|
|
|
import type { PageServerLoad } from './$types';
|
2023-05-21 05:18:04 +00:00
|
|
|
|
2024-03-01 01:11:07 +00:00
|
|
|
const signUpDefaults = {
|
2024-11-23 22:49:16 +00:00
|
|
|
firstName: '',
|
|
|
|
|
lastName: '',
|
|
|
|
|
email: '',
|
|
|
|
|
username: '',
|
|
|
|
|
password: '',
|
|
|
|
|
confirm_password: '',
|
|
|
|
|
terms: true,
|
2024-11-09 19:05:28 +00:00
|
|
|
};
|
2024-03-01 01:11:07 +00:00
|
|
|
|
2023-11-05 06:20:34 +00:00
|
|
|
export const load: PageServerLoad = async (event) => {
|
2024-11-23 22:49:16 +00:00
|
|
|
const { locals } = event;
|
2024-03-02 02:00:27 +00:00
|
|
|
|
2024-11-23 22:49:16 +00:00
|
|
|
const { user } = await locals.getAuthedUser();
|
2024-08-08 19:38:17 +00:00
|
|
|
|
2024-11-23 22:49:16 +00:00
|
|
|
if (user) {
|
|
|
|
|
const message = { type: 'success', message: 'You are already signed in' } as const;
|
|
|
|
|
throw redirect('/', message, event);
|
|
|
|
|
}
|
2024-03-02 02:00:27 +00:00
|
|
|
|
2024-11-23 22:49:16 +00:00
|
|
|
return {
|
|
|
|
|
signupForm: await superValidate(zod(signupUsernameEmailDto), {
|
|
|
|
|
defaults: signUpDefaults,
|
|
|
|
|
}),
|
|
|
|
|
};
|
2024-11-09 19:05:28 +00:00
|
|
|
};
|
2023-05-21 05:18:04 +00:00
|
|
|
|
2023-11-05 06:20:34 +00:00
|
|
|
export const actions: Actions = {
|
2024-11-23 22:49:16 +00:00
|
|
|
default: async (event) => {
|
|
|
|
|
const { locals } = event;
|
2024-02-08 01:16:17 +00:00
|
|
|
|
2024-11-23 22:49:16 +00:00
|
|
|
const { user } = await locals.getAuthedUser();
|
2024-02-08 01:16:17 +00:00
|
|
|
|
2024-11-23 22:49:16 +00:00
|
|
|
if (user) {
|
|
|
|
|
const message = { type: 'success', message: 'You are already signed in' } as const;
|
|
|
|
|
throw redirect('/', message, event);
|
|
|
|
|
}
|
2023-12-15 01:53:15 +00:00
|
|
|
|
2024-11-23 22:49:16 +00:00
|
|
|
const form = await superValidate(event, zod(signupUsernameEmailDto));
|
2023-05-21 05:18:04 +00:00
|
|
|
|
2024-11-23 22:49:16 +00:00
|
|
|
const { error } = await locals.api.signup.$post({ json: form.data }).then(locals.parseApiResponse);
|
|
|
|
|
if (error) {
|
2024-11-09 19:05:28 +00:00
|
|
|
form.data.password = '';
|
2024-11-23 22:49:16 +00:00
|
|
|
return setError(form, 'username', 'Unable to log in.');
|
|
|
|
|
}
|
2023-05-21 05:18:04 +00:00
|
|
|
|
2024-11-23 22:49:16 +00:00
|
|
|
if (!form.valid) {
|
|
|
|
|
form.data.password = '';
|
|
|
|
|
form.data.confirm_password = '';
|
|
|
|
|
return fail(400, {
|
|
|
|
|
form,
|
|
|
|
|
});
|
|
|
|
|
}
|
2024-01-19 00:57:15 +00:00
|
|
|
|
2024-11-23 22:49:16 +00:00
|
|
|
redirect(302, '/');
|
|
|
|
|
},
|
2024-11-09 19:05:28 +00:00
|
|
|
};
|