2024-02-07 01:08:03 +00:00
|
|
|
import { fail, error, type Actions, redirect } from '@sveltejs/kit';
|
2024-02-09 02:56:09 +00:00
|
|
|
import { Argon2id } from 'oslo/password';
|
|
|
|
|
import { eq } from 'drizzle-orm';
|
|
|
|
|
import { nanoid } from 'nanoid';
|
2024-02-08 01:53:02 +00:00
|
|
|
import { setError, superValidate } from 'sveltekit-superforms/server';
|
2023-11-05 06:20:34 +00:00
|
|
|
import type { PageServerLoad } from './$types';
|
2023-12-15 01:53:15 +00:00
|
|
|
import { lucia } from '$lib/server/auth';
|
2024-02-17 08:10:19 +00:00
|
|
|
import { signUpSchema } 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';
|
2024-02-07 01:08:03 +00:00
|
|
|
import db from '$lib/drizzle';
|
2024-02-08 01:16:17 +00:00
|
|
|
import { collections, users, wishlists } from '../../../schema';
|
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-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
|
2024-02-08 01:53:02 +00:00
|
|
|
console.log('Check if user already exists');
|
2024-02-08 01:16:17 +00:00
|
|
|
|
2024-02-08 01:53:02 +00:00
|
|
|
const existing_user = await db.query
|
|
|
|
|
.users
|
|
|
|
|
.findFirst({ where: eq(users.username, form.data.username) });
|
2024-02-08 01:16:17 +00:00
|
|
|
|
2024-02-08 01:53:02 +00:00
|
|
|
if (existing_user) {
|
|
|
|
|
return setError(form, 'username', 'You cannot create an account with that username');
|
|
|
|
|
}
|
2024-02-08 01:16:17 +00:00
|
|
|
|
2024-02-08 01:53:02 +00:00
|
|
|
console.log('Creating user');
|
2023-05-21 05:18:04 +00:00
|
|
|
|
2024-02-08 01:53:02 +00:00
|
|
|
const hashedPassword = await new Argon2id().hash(form.data.password);
|
2023-12-15 01:53:15 +00:00
|
|
|
|
2024-02-09 02:56:09 +00:00
|
|
|
const user = await db.insert(users)
|
2024-02-08 01:53:02 +00:00
|
|
|
.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'
|
2024-02-09 02:56:09 +00:00
|
|
|
}).returning();
|
2024-02-08 01:53:02 +00:00
|
|
|
console.log('signup user', user);
|
2023-05-21 05:18:04 +00:00
|
|
|
|
2024-02-08 01:53:02 +00:00
|
|
|
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: ${nanoid()}`
|
|
|
|
|
});
|
|
|
|
|
}
|
2023-05-21 05:18:04 +00:00
|
|
|
|
2024-02-08 01:53:02 +00:00
|
|
|
add_user_to_role(user[0].id, 'user');
|
|
|
|
|
await db.insert(collections)
|
|
|
|
|
.values({
|
|
|
|
|
user_id: user[0].id
|
|
|
|
|
});
|
|
|
|
|
await db.insert(wishlists)
|
|
|
|
|
.values({
|
|
|
|
|
user_id: user[0].id
|
|
|
|
|
});
|
2024-02-08 01:16:17 +00:00
|
|
|
|
2024-02-08 01:53:02 +00:00
|
|
|
try {
|
2024-02-08 01:16:17 +00:00
|
|
|
session = await lucia.createSession(user[0].id, {
|
2024-02-19 08:22:05 +00:00
|
|
|
ip_country: event.locals.ip,
|
|
|
|
|
ip_address: event.locals.country
|
2024-02-08 01:16:17 +00:00
|
|
|
});
|
|
|
|
|
sessionCookie = lucia.createSessionCookie(session.id);
|
2023-12-15 01:53:15 +00:00
|
|
|
} 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
|
|
|
|
2024-02-08 01:16:17 +00:00
|
|
|
event.cookies.set(sessionCookie.name, sessionCookie.value, {
|
|
|
|
|
path: ".",
|
|
|
|
|
...sessionCookie.attributes
|
|
|
|
|
});
|
2024-01-19 00:57:15 +00:00
|
|
|
|
2024-02-08 01:53:02 +00:00
|
|
|
redirect(302, '/');
|
|
|
|
|
// const message = { type: 'success', message: 'Signed Up!' } as const;
|
|
|
|
|
// throw flashRedirect(message, event);
|
2023-05-21 05:18:04 +00:00
|
|
|
}
|
|
|
|
|
};
|