'use server' import { RegisterSchema } from '@/schemas' import { infer as zInfer } from 'zod' import bcrypt from 'bcryptjs' import { PASSWORD_SALT_LENGTH } from '@/config/validation' import db from '@/lib/db' import { getUserByEmail } from '@/data/user' import { sendVerificationEmail } from '@/actions/send-verification-email' export const register = async (values: zInfer) => { const validatedFields = RegisterSchema.safeParse(values) if (!validatedFields.success) { return { error: 'auth.form.error.invalid_fields' } } const { email, password, name } = validatedFields.data const hashedPassword = await bcrypt.hash(password, PASSWORD_SALT_LENGTH) const existingUser = await getUserByEmail(email) if (existingUser) { return { error: 'auth.form.error.email_taken' } } await db.user.create({ data: { name, email, password: hashedPassword, }, }) return await sendVerificationEmail(email, name) }