28 lines
860 B
TypeScript
28 lines
860 B
TypeScript
import { prisma, uuid } from '@app/server/prisma/client';
|
|
import { RegisterInput } from '@generated/types';
|
|
import bcrypt from 'bcrypt';
|
|
|
|
export async function SignUpResolver(_: any, input: RegisterInput): Promise<void> {
|
|
const existingUsername = await prisma.user.findUnique({
|
|
where: { username: input.username },
|
|
select: { id: true }
|
|
});
|
|
if (existingUsername != null) throw Error('User with this username already exists.');
|
|
|
|
const existingEmail = await prisma.user.findUnique({
|
|
where: { email: input.email },
|
|
select: { id: true }
|
|
});
|
|
if (existingEmail != null) throw Error('Email is already in use.');
|
|
|
|
const hashedPassword = await bcrypt.hash(input.password, 10);
|
|
|
|
await prisma.user.create({
|
|
data: {
|
|
id: uuid(),
|
|
email: input.email,
|
|
username: input.username,
|
|
password: hashedPassword
|
|
}
|
|
});
|
|
}
|