MeetUp/src/lib/auth/register.ts
Dominik Stahl afbefa8a52
Some checks failed
docker-build / docker (pull_request) Has been cancelled
container-scan / Container Scan (pull_request) Has been cancelled
refactor(validation): restucture api input and output validation
2025-06-18 23:16:58 +02:00

53 lines
1.3 KiB
TypeScript

'use server';
import type { z } from 'zod/v4';
import bcrypt from 'bcryptjs';
import { registerServerSchema } from './validation';
import { prisma } from '@/prisma';
export async function registerAction(
data: z.infer<typeof registerServerSchema>,
) {
try {
const result = await registerServerSchema.safeParseAsync(data);
if (!result.success) {
return {
error: result.error.issues[0].message,
};
}
const { email, password, firstName, lastName, username } = result.data;
const passwordHash = await bcrypt.hash(password, 10);
await prisma.$transaction(async (tx) => {
const { id } = await tx.user.create({
data: {
email,
name: username,
password_hash: passwordHash,
first_name: firstName,
last_name: lastName,
emailVerified: new Date(), // TODO: handle email verification
},
});
await tx.account.create({
data: {
userId: id,
type: 'credentials',
provider: 'credentials',
providerAccountId: id,
},
});
});
return {};
// eslint-disable-next-line @typescript-eslint/no-unused-vars
} catch (_error) {
return {
error: 'System error. Please contact support',
};
}
}