2023-12-05 06:25:43 +00:00
|
|
|
import {fail, error, type Actions, redirect} from '@sveltejs/kit';
|
2023-10-17 09:28:53 +00:00
|
|
|
import { superValidate } from 'sveltekit-superforms/server';
|
2023-11-05 06:20:34 +00:00
|
|
|
import type { PageServerLoad } from './$types';
|
2023-11-05 00:03:28 +00:00
|
|
|
import prisma from '$lib/prisma';
|
2023-12-15 01:53:15 +00:00
|
|
|
import { lucia } from '$lib/server/auth';
|
|
|
|
|
import { Argon2id } from 'oslo/password';
|
2023-05-21 05:18:04 +00:00
|
|
|
import { userSchema } from '$lib/config/zod-schemas';
|
2023-07-30 23:31:39 +00:00
|
|
|
import { add_user_to_role } from '$server/roles';
|
|
|
|
|
import type { Message } from '$lib/types.js';
|
2023-05-21 05:18:04 +00:00
|
|
|
|
2023-06-20 18:55:21 +00:00
|
|
|
const signUpSchema = userSchema
|
|
|
|
|
.pick({
|
|
|
|
|
firstName: true,
|
|
|
|
|
lastName: true,
|
|
|
|
|
email: true,
|
|
|
|
|
username: true,
|
|
|
|
|
password: true,
|
|
|
|
|
confirm_password: true,
|
|
|
|
|
terms: true
|
|
|
|
|
})
|
|
|
|
|
.superRefine(({ confirm_password, password }, ctx) => {
|
|
|
|
|
if (confirm_password !== password) {
|
|
|
|
|
ctx.addIssue({
|
|
|
|
|
code: 'custom',
|
|
|
|
|
message: 'Password and Confirm Password must match',
|
|
|
|
|
path: ['confirm_password']
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
2023-05-21 05:18:04 +00:00
|
|
|
|
2023-11-05 06:20:34 +00:00
|
|
|
export const load: PageServerLoad = async (event) => {
|
2023-07-30 05:00:51 +00:00
|
|
|
console.log('sign up load event', event);
|
2023-11-05 00:03:28 +00:00
|
|
|
// const session = await event.locals.auth.validate();
|
|
|
|
|
// if (session) {
|
|
|
|
|
// throw redirect(302, '/');
|
|
|
|
|
// }
|
2023-05-21 05:18:04 +00:00
|
|
|
return {
|
2023-09-08 23:30:32 +00:00
|
|
|
form: await superValidate<typeof signUpSchema, Message>(event, signUpSchema)
|
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) => {
|
2023-12-15 01:53:15 +00:00
|
|
|
const { cookies } = event;
|
2023-07-30 23:31:39 +00:00
|
|
|
const form = await superValidate<typeof signUpSchema, Message>(event, signUpSchema);
|
2023-05-21 05:18:04 +00:00
|
|
|
if (!form.valid) {
|
2023-07-31 05:24:33 +00:00
|
|
|
form.data.password = '';
|
|
|
|
|
form.data.confirm_password = '';
|
2023-05-21 05:18:04 +00:00
|
|
|
return fail(400, {
|
|
|
|
|
form
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-15 01:53:15 +00:00
|
|
|
let session;
|
|
|
|
|
let sessionCookie;
|
2023-05-21 05:18:04 +00:00
|
|
|
// Adding user to the db
|
|
|
|
|
try {
|
|
|
|
|
console.log('Creating user');
|
|
|
|
|
|
2023-12-15 01:53:15 +00:00
|
|
|
const hashedPassword = await new Argon2id().hash(form.data.password);
|
|
|
|
|
|
|
|
|
|
const user = await prisma.user.create({
|
|
|
|
|
data: {
|
2023-05-21 05:18:04 +00:00
|
|
|
username: form.data.username,
|
2023-12-15 01:53:15 +00:00
|
|
|
hashed_password: hashedPassword,
|
|
|
|
|
email: form.data.email || '',
|
2023-05-21 05:18:04 +00:00
|
|
|
firstName: form.data.firstName || '',
|
|
|
|
|
lastName: form.data.lastName || '',
|
|
|
|
|
verified: false,
|
|
|
|
|
receiveEmail: false,
|
2023-12-15 01:53:15 +00:00
|
|
|
theme: 'system'
|
2023-05-21 05:18:04 +00:00
|
|
|
}
|
|
|
|
|
});
|
2023-07-30 05:00:51 +00:00
|
|
|
console.log('signup user', user);
|
2023-12-15 01:53:15 +00:00
|
|
|
add_user_to_role(user.id, 'user');
|
2023-11-05 00:03:28 +00:00
|
|
|
await prisma.collection.create({
|
2023-07-18 21:23:45 +00:00
|
|
|
data: {
|
2023-12-15 01:53:15 +00:00
|
|
|
user_id: user.id
|
2023-07-18 21:23:45 +00:00
|
|
|
}
|
|
|
|
|
});
|
2023-11-05 00:03:28 +00:00
|
|
|
await prisma.wishlist.create({
|
2023-07-18 21:23:45 +00:00
|
|
|
data: {
|
2023-12-15 01:53:15 +00:00
|
|
|
user_id: user.id
|
2023-07-18 21:23:45 +00:00
|
|
|
}
|
|
|
|
|
});
|
2023-05-21 05:18:04 +00:00
|
|
|
|
|
|
|
|
console.log('User', user);
|
|
|
|
|
|
2023-12-15 01:53:15 +00:00
|
|
|
session = await lucia.createSession(user.id, {
|
|
|
|
|
country: event.locals.session.country
|
2023-07-30 05:00:51 +00:00
|
|
|
});
|
2023-12-15 01:53:15 +00:00
|
|
|
sessionCookie = lucia.createSessionCookie(session.id);
|
|
|
|
|
} catch (e: any) {
|
|
|
|
|
if (e.message.toUpperCase() === `DUPLICATE_KEY_ID`) {
|
2023-07-30 05:00:51 +00:00
|
|
|
// key already exists
|
2023-07-30 23:31:39 +00:00
|
|
|
console.error('Lucia Error: ', e);
|
2023-07-30 05:00:51 +00:00
|
|
|
}
|
2023-07-30 23:31:39 +00:00
|
|
|
console.log(e);
|
|
|
|
|
const message = {
|
|
|
|
|
type: 'error',
|
|
|
|
|
message: 'Unable to create your account. Please try again.'
|
|
|
|
|
};
|
2023-07-31 01:18:39 +00:00
|
|
|
form.data.password = '';
|
|
|
|
|
form.data.confirm_password = '';
|
2023-12-27 01:26:39 +00:00
|
|
|
error(500, message);
|
2023-05-21 05:18:04 +00:00
|
|
|
}
|
2023-12-15 01:53:15 +00:00
|
|
|
|
|
|
|
|
event.cookies.set(sessionCookie.name, sessionCookie.value, sessionCookie.attributes);
|
2023-12-27 01:26:39 +00:00
|
|
|
redirect(302, '/');
|
2023-12-05 06:25:43 +00:00
|
|
|
// const message = { type: 'success', message: 'Signed Up!' } as const;
|
|
|
|
|
// throw flashRedirect(message, event);
|
2023-05-21 05:18:04 +00:00
|
|
|
}
|
|
|
|
|
};
|