70 lines
1.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { NextResponse } from 'next/server'
import { hash } from 'bcryptjs'
import { prisma } from '@/lib/prisma'
export async function POST(req: Request) {
try {
const { name, email, password } = await req.json()
// Gerekli alanların kontrolü
if (!name || !email || !password) {
return NextResponse.json(
{ message: 'Tüm alanların doldurulması zorunludur' },
{ status: 400 }
)
}
// Email formatı kontrolü
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
if (!emailRegex.test(email)) {
return NextResponse.json(
{ message: 'Geçerli bir email adresi giriniz' },
{ status: 400 }
)
}
// Email kullanımda mı kontrolü
const existingUser = await prisma.user.findUnique({
where: { email },
})
if (existingUser) {
return NextResponse.json(
{ message: 'Bu email adresi zaten kullanımda' },
{ status: 400 }
)
}
// Şifre hash'leme
const hashedPassword = await hash(password, 12)
// Kullanıcı oluşturma
const user = await prisma.user.create({
data: {
name,
email,
password: hashedPassword,
role: 'POSTACI', // Varsayılan rol
},
})
return NextResponse.json(
{
message: 'Kullanıcı başarıyla oluşturuldu',
user: {
id: user.id,
name: user.name,
email: user.email,
role: user.role,
},
},
{ status: 201 }
)
} catch (error) {
console.error('Kayıt hatası:', error)
return NextResponse.json(
{ message: 'Kayıt işlemi sırasında bir hata oluştu' },
{ status: 500 }
)
}
}