35 lines
970 B
TypeScript
35 lines
970 B
TypeScript
'use server'
|
|
|
|
import { infer as zInfer } from 'zod'
|
|
import bcrypt from 'bcryptjs'
|
|
|
|
import { RegisterSchema } from '@/schemas'
|
|
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<typeof RegisterSchema>) => {
|
|
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)
|
|
} |