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 = {
|
|
|
|
|
firstName: '',
|
|
|
|
|
lastName: '',
|
|
|
|
|
email: '',
|
|
|
|
|
username: '',
|
|
|
|
|
password: '',
|
|
|
|
|
confirm_password: '',
|
2024-05-08 00:19:13 +00:00
|
|
|
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-09 19:05:28 +00:00
|
|
|
const { locals } = event;
|
2024-03-02 02:00:27 +00:00
|
|
|
|
2024-11-09 19:05:28 +00:00
|
|
|
const authedUser = await locals.getAuthedUser();
|
2024-08-08 19:38:17 +00:00
|
|
|
|
|
|
|
|
if (authedUser) {
|
2024-11-09 19:05:28 +00:00
|
|
|
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-08-08 19:38:17 +00:00
|
|
|
// if (userFullyAuthenticated(user, session)) {
|
|
|
|
|
// const message = { type: 'success', message: 'You are already signed in' } as const;
|
|
|
|
|
// throw redirect('/', message, event);
|
|
|
|
|
// } else if (userNotFullyAuthenticated(user, session)) {
|
|
|
|
|
// try {
|
|
|
|
|
// await lucia.invalidateSession(locals.session!.id!);
|
|
|
|
|
// } catch (error) {
|
|
|
|
|
// console.log('Session already invalidated');
|
|
|
|
|
// }
|
|
|
|
|
// const sessionCookie = lucia.createBlankSessionCookie();
|
|
|
|
|
// cookies.set(sessionCookie.name, sessionCookie.value, {
|
|
|
|
|
// path: '.',
|
|
|
|
|
// ...sessionCookie.attributes,
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
|
2023-05-21 05:18:04 +00:00
|
|
|
return {
|
2024-08-15 23:25:41 +00:00
|
|
|
form: await superValidate(zod(signupUsernameEmailDto), {
|
2024-05-08 00:19:13 +00:00
|
|
|
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 = {
|
2023-05-21 05:18:04 +00:00
|
|
|
default: async (event) => {
|
2024-11-09 19:05:28 +00:00
|
|
|
const { locals } = event;
|
2023-05-21 05:18:04 +00:00
|
|
|
|
2024-11-09 19:05:28 +00:00
|
|
|
const authedUser = await locals.getAuthedUser();
|
2024-02-08 01:16:17 +00:00
|
|
|
|
2024-08-15 23:25:41 +00:00
|
|
|
if (authedUser) {
|
2024-11-09 19:05:28 +00:00
|
|
|
const message = { type: 'success', message: 'You are already signed in' } as const;
|
|
|
|
|
throw redirect('/', message, event);
|
2024-02-08 01:53:02 +00:00
|
|
|
}
|
2024-02-08 01:16:17 +00:00
|
|
|
|
2024-11-09 19:05:28 +00:00
|
|
|
const form = await superValidate(event, zod(signupUsernameEmailDto));
|
2023-12-15 01:53:15 +00:00
|
|
|
|
2024-11-09 19:05:28 +00:00
|
|
|
const { error } = await locals.api.signup.$post({ json: form.data }).then(locals.parseApiResponse);
|
|
|
|
|
if (error) return setError(form, 'username', error);
|
2023-05-21 05:18:04 +00:00
|
|
|
|
2024-08-15 23:25:41 +00:00
|
|
|
if (!form.valid) {
|
2024-11-09 19:05:28 +00:00
|
|
|
form.data.password = '';
|
|
|
|
|
form.data.confirm_password = '';
|
2024-02-08 01:53:02 +00:00
|
|
|
return fail(400, {
|
|
|
|
|
form,
|
2024-11-09 19:05:28 +00:00
|
|
|
});
|
2024-02-08 01:53:02 +00:00
|
|
|
}
|
2023-05-21 05:18:04 +00:00
|
|
|
|
2024-08-15 23:25:41 +00:00
|
|
|
// let session;
|
|
|
|
|
// let sessionCookie;
|
|
|
|
|
// // Adding user to the db
|
|
|
|
|
// console.log('Check if user already exists');
|
|
|
|
|
//
|
|
|
|
|
// const existing_user = await db.query.usersTable.findFirst({
|
|
|
|
|
// where: eq(usersTable.username, form.data.username),
|
|
|
|
|
// });
|
|
|
|
|
//
|
|
|
|
|
// if (existing_user) {
|
|
|
|
|
// return setError(form, 'username', 'You cannot create an account with that username');
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// console.log('Creating user');
|
|
|
|
|
//
|
|
|
|
|
// const hashedPassword = await new Argon2id().hash(form.data.password);
|
|
|
|
|
//
|
|
|
|
|
// const user = await db
|
|
|
|
|
// .insert(usersTable)
|
|
|
|
|
// .values({
|
|
|
|
|
// username: form.data.username,
|
|
|
|
|
// hashed_password: hashedPassword,
|
|
|
|
|
// email: form.data.email,
|
|
|
|
|
// first_name: form.data.firstName ?? '',
|
|
|
|
|
// last_name: form.data.lastName ?? '',
|
|
|
|
|
// verified: false,
|
|
|
|
|
// receive_email: false,
|
|
|
|
|
// theme: 'system',
|
|
|
|
|
// })
|
|
|
|
|
// .returning();
|
|
|
|
|
// console.log('signup user', user);
|
|
|
|
|
//
|
|
|
|
|
// if (!user || user.length === 0) {
|
|
|
|
|
// return fail(400, {
|
|
|
|
|
// form,
|
|
|
|
|
// message: `Could not create your account. Please try again. If the problem persists, please contact support. Error ID: ${cuid2()}`,
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// await add_user_to_role(user[0].id, 'user', true);
|
|
|
|
|
// await db.insert(collections).values({
|
|
|
|
|
// user_id: user[0].id,
|
|
|
|
|
// });
|
2024-09-04 23:04:41 +00:00
|
|
|
// await db.insert(wishlistsTable).values({
|
2024-08-15 23:25:41 +00:00
|
|
|
// user_id: user[0].id,
|
|
|
|
|
// });
|
|
|
|
|
//
|
|
|
|
|
// try {
|
|
|
|
|
// session = await lucia.createSession(user[0].id, {
|
|
|
|
|
// ip_country: event.locals.ip,
|
|
|
|
|
// ip_address: event.locals.country,
|
|
|
|
|
// twoFactorAuthEnabled: false,
|
|
|
|
|
// isTwoFactorAuthenticated: false,
|
|
|
|
|
// });
|
|
|
|
|
// sessionCookie = lucia.createSessionCookie(session.id);
|
|
|
|
|
// } catch (e: any) {
|
|
|
|
|
// if (e.message.toUpperCase() === `DUPLICATE_KEY_ID`) {
|
|
|
|
|
// // key already exists
|
|
|
|
|
// console.error('Lucia Error: ', e);
|
|
|
|
|
// }
|
|
|
|
|
// console.log(e);
|
|
|
|
|
// const message = {
|
|
|
|
|
// type: 'error',
|
|
|
|
|
// message: 'Unable to create your account. Please try again.',
|
|
|
|
|
// };
|
|
|
|
|
// form.data.password = '';
|
|
|
|
|
// form.data.confirm_password = '';
|
|
|
|
|
// error(500, message);
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// event.cookies.set(sessionCookie.name, sessionCookie.value, {
|
|
|
|
|
// path: '.',
|
|
|
|
|
// ...sessionCookie.attributes,
|
|
|
|
|
// });
|
2024-01-19 00:57:15 +00:00
|
|
|
|
2024-11-09 19:05:28 +00:00
|
|
|
redirect(302, '/');
|
2024-02-08 01:53:02 +00:00
|
|
|
// const message = { type: 'success', message: 'Signed Up!' } as const;
|
|
|
|
|
// throw flashRedirect(message, event);
|
2024-05-08 00:19:13 +00:00
|
|
|
},
|
2024-11-09 19:05:28 +00:00
|
|
|
};
|