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

115 lines
2.9 KiB
TypeScript
Raw Normal View History

import {fail, error, type Actions, redirect} from '@sveltejs/kit';
import { superValidate } from 'sveltekit-superforms/server';
import { LuciaError } from 'lucia';
import type { PageServerLoad } from './$types';
import prisma from '$lib/prisma';
2023-05-21 05:18:04 +00:00
import { auth } from '$lib/server/lucia';
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 form = await superValidate<typeof signUpSchema, Message>(event, signUpSchema);
debugger;
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
});
}
// Adding user to the db
try {
console.log('Creating user');
const token = crypto.randomUUID();
const user = await auth.createUser({
key: {
2023-05-21 05:18:04 +00:00
providerId: 'username',
providerUserId: form.data.username,
password: form.data.password
},
attributes: {
email: form.data.email || null,
2023-05-21 05:18:04 +00:00
username: form.data.username,
firstName: form.data.firstName || '',
lastName: form.data.lastName || '',
verified: false,
receiveEmail: false,
theme: 'system',
2023-05-21 05:18:04 +00:00
token
}
});
console.log('signup user', user);
add_user_to_role(user.userId, 'user');
await prisma.collection.create({
data: {
user_id: user.userId
}
});
await prisma.wishlist.create({
data: {
user_id: user.userId
}
});
2023-05-21 05:18:04 +00:00
console.log('User', user);
const session = await auth.createSession({
userId: user.userId,
attributes: {}
});
2023-05-21 05:18:04 +00:00
event.locals.auth.setSession(session);
} catch (e) {
if (e instanceof LuciaError && 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
}
throw redirect(302, '/');
// const message = { type: 'success', message: 'Signed Up!' } as const;
// throw flashRedirect(message, event);
2023-05-21 05:18:04 +00:00
}
};