boredgame/src/routes/(auth)/sign-up/+page.server.ts

114 lines
3 KiB
TypeScript
Raw Normal View History

import {fail, error, type Actions, redirect} from '@sveltejs/kit';
import { superValidate } from 'sveltekit-superforms/server';
import type { PageServerLoad } from './$types';
import prisma from '$lib/prisma';
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';
import { add_user_to_role } from '$server/roles';
import type { Message } from '$lib/types.js';
2023-05-21 05:18:04 +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
export const load: PageServerLoad = async (event) => {
console.log('sign up load event', event);
// const session = await event.locals.auth.validate();
// if (session) {
// throw redirect(302, '/');
// }
2023-05-21 05:18:04 +00:00
return {
form: await superValidate<typeof signUpSchema, Message>(event, signUpSchema)
2023-05-21 05:18:04 +00:00
};
};
export const actions: Actions = {
2023-05-21 05:18:04 +00:00
default: async (event) => {
const { cookies } = event;
const form = await superValidate<typeof signUpSchema, Message>(event, signUpSchema);
2023-05-21 05:18:04 +00:00
if (!form.valid) {
form.data.password = '';
form.data.confirm_password = '';
2023-05-21 05:18:04 +00:00
return fail(400, {
form
});
}
let session;
let sessionCookie;
2023-05-21 05:18:04 +00:00
// Adding user to the db
try {
console.log('Creating user');
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,
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,
theme: 'system'
2023-05-21 05:18:04 +00:00
}
});
console.log('signup user', user);
add_user_to_role(user.id, 'user');
await prisma.collection.create({
data: {
user_id: user.id
}
});
await prisma.wishlist.create({
data: {
user_id: user.id
}
});
2023-05-21 05:18:04 +00:00
console.log('User', user);
session = await lucia.createSession(user.id, {
country: event.locals.session.country
});
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 = '';
throw error(500, message);
2023-05-21 05:18:04 +00:00
}
event.cookies.set(sessionCookie.name, sessionCookie.value, sessionCookie.attributes);
throw redirect(302, '/');
// const message = { type: 'success', message: 'Signed Up!' } as const;
// throw flashRedirect(message, event);
2023-05-21 05:18:04 +00:00
}
};