added mail service
This commit is contained in:
35
actions/register.ts
Normal file
35
actions/register.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
'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)
|
||||
}
|
||||
Reference in New Issue
Block a user