2023-05-21 05:18:04 +00:00
|
|
|
import { fail, redirect } from '@sveltejs/kit';
|
|
|
|
|
import { setError, superValidate } from 'sveltekit-superforms/server';
|
2023-07-30 05:00:51 +00:00
|
|
|
import { redirect as flashRedirect } from 'sveltekit-flash-message/server';
|
|
|
|
|
import { LuciaError } from 'lucia';
|
2023-05-21 05:18:04 +00:00
|
|
|
import { auth } from '$lib/server/lucia';
|
|
|
|
|
import { userSchema } from '$lib/config/zod-schemas';
|
2023-06-16 06:28:49 +00:00
|
|
|
import { add_user_to_role } from '$db/roles';
|
2023-07-18 21:23:45 +00:00
|
|
|
import prisma from '$lib/prisma.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: ['password']
|
|
|
|
|
// });
|
|
|
|
|
ctx.addIssue({
|
|
|
|
|
code: 'custom',
|
|
|
|
|
message: 'Password and Confirm Password must match',
|
|
|
|
|
path: ['confirm_password']
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
2023-05-21 05:18:04 +00:00
|
|
|
|
|
|
|
|
export const load = async (event) => {
|
2023-07-30 05:00:51 +00:00
|
|
|
console.log('sign up load event', event);
|
2023-05-21 05:18:04 +00:00
|
|
|
const session = await event.locals.auth.validate();
|
|
|
|
|
if (session) {
|
|
|
|
|
throw redirect(302, '/');
|
|
|
|
|
}
|
|
|
|
|
const form = await superValidate(event, signUpSchema);
|
|
|
|
|
return {
|
|
|
|
|
form
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const actions = {
|
|
|
|
|
default: async (event) => {
|
|
|
|
|
const form = await superValidate(event, signUpSchema);
|
|
|
|
|
|
|
|
|
|
if (!form.valid) {
|
|
|
|
|
return fail(400, {
|
|
|
|
|
form
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Adding user to the db
|
|
|
|
|
try {
|
|
|
|
|
console.log('Creating user');
|
|
|
|
|
const token = crypto.randomUUID();
|
|
|
|
|
|
|
|
|
|
const user = await auth.createUser({
|
2023-07-30 05:00:51 +00:00
|
|
|
key: {
|
2023-05-21 05:18:04 +00:00
|
|
|
providerId: 'username',
|
|
|
|
|
providerUserId: form.data.username,
|
|
|
|
|
password: form.data.password
|
|
|
|
|
},
|
|
|
|
|
attributes: {
|
|
|
|
|
email: form.data.email || '',
|
|
|
|
|
username: form.data.username,
|
|
|
|
|
firstName: form.data.firstName || '',
|
|
|
|
|
lastName: form.data.lastName || '',
|
|
|
|
|
verified: false,
|
|
|
|
|
receiveEmail: false,
|
2023-06-20 18:55:21 +00:00
|
|
|
theme: 'system',
|
2023-05-21 05:18:04 +00:00
|
|
|
token
|
|
|
|
|
}
|
|
|
|
|
});
|
2023-07-30 05:00:51 +00:00
|
|
|
console.log('signup user', user);
|
|
|
|
|
add_user_to_role(user.userId, 'user');
|
2023-07-18 21:23:45 +00:00
|
|
|
await prisma.collection.create({
|
|
|
|
|
data: {
|
2023-07-30 05:00:51 +00:00
|
|
|
user_id: user.userId
|
2023-07-18 21:23:45 +00:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
await prisma.wishlist.create({
|
|
|
|
|
data: {
|
2023-07-30 05:00:51 +00:00
|
|
|
user_id: user.userId,
|
2023-07-18 21:23:45 +00:00
|
|
|
name: 'My Wishlist'
|
|
|
|
|
}
|
|
|
|
|
});
|
2023-05-21 05:18:04 +00:00
|
|
|
|
|
|
|
|
console.log('User', user);
|
|
|
|
|
|
2023-07-30 05:00:51 +00:00
|
|
|
const session = await auth.createSession({
|
|
|
|
|
userId: user.userId,
|
|
|
|
|
attributes: {}
|
|
|
|
|
});
|
2023-05-21 05:18:04 +00:00
|
|
|
event.locals.auth.setSession(session);
|
2023-07-30 05:00:51 +00:00
|
|
|
// const message = { type: 'success', message: 'Signed Up!' } as const;
|
|
|
|
|
// throw flashRedirect(message, event);
|
2023-05-21 05:18:04 +00:00
|
|
|
} catch (error) {
|
2023-07-30 05:00:51 +00:00
|
|
|
if (error instanceof LuciaError && error.message === `DUPLICATE_KEY_ID`) {
|
|
|
|
|
// key already exists
|
|
|
|
|
console.error(error);
|
|
|
|
|
}
|
2023-05-21 05:18:04 +00:00
|
|
|
console.log(error);
|
2023-06-20 18:55:21 +00:00
|
|
|
return setError(form, '', 'Unable to create your account. Please try again.');
|
2023-05-21 05:18:04 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|