boredgame/src/routes/auth/signup/+page.server.ts

119 lines
2.9 KiB
TypeScript
Raw Normal View History

import { fail, redirect, error } from '@sveltejs/kit';
2023-05-21 05:18:04 +00:00
import { setError, superValidate } from 'sveltekit-superforms/server';
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';
import { add_user_to_role } from '$server/roles';
import prisma from '$lib/prisma.js';
import { Schema } from 'zod';
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: ['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) => {
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<typeof signUpSchema, Message>(event, signUpSchema);
2023-05-21 05:18:04 +00:00
return {
form
};
};
export const actions = {
default: async (event) => {
const form = await superValidate<typeof signUpSchema, Message>(event, signUpSchema);
2023-05-21 05:18:04 +00:00
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({
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,
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,
name: 'My Wishlist'
}
});
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);
// const message = { type: 'success', message: 'Signed Up!' } as const;
// throw flashRedirect(message, event);
} catch (e) {
if (e instanceof LuciaError && e.message === `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.'
};
throw error(500, message);
// return setError(form, '', message);
2023-05-21 05:18:04 +00:00
}
}
};