first commit
This commit is contained in:
3
app/api/auth/[...nextauth]/route.ts
Normal file
3
app/api/auth/[...nextauth]/route.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
import { handlers } from '@/auth'
|
||||
|
||||
export const { GET, POST } = handlers
|
||||
72
app/api/auth/signup/route.ts
Normal file
72
app/api/auth/signup/route.ts
Normal file
@@ -0,0 +1,72 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { hash } from 'bcryptjs'
|
||||
import { prisma } from '@/lib/prisma'
|
||||
import { signUpSchema } from '@/lib/validations/auth'
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const body = await request.json()
|
||||
const { name, email, password, phone, referralCode, role } = signUpSchema.parse(body)
|
||||
|
||||
// Check if user already exists
|
||||
const existingUser = await prisma.user.findUnique({
|
||||
where: { email },
|
||||
})
|
||||
|
||||
if (existingUser) {
|
||||
return NextResponse.json(
|
||||
{ error: 'User already exists' },
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
|
||||
// Hash password
|
||||
const hashedPassword = await hash(password, 12)
|
||||
|
||||
// Find referrer if referral code is provided
|
||||
let referrerId = null
|
||||
if (referralCode) {
|
||||
const referrer = await prisma.user.findUnique({
|
||||
where: { referralCode },
|
||||
})
|
||||
if (referrer) {
|
||||
referrerId = referrer.id
|
||||
}
|
||||
}
|
||||
|
||||
// Create user
|
||||
const user = await prisma.user.create({
|
||||
data: {
|
||||
name,
|
||||
email,
|
||||
password: hashedPassword,
|
||||
phone,
|
||||
role: role || 'CUSTOMER',
|
||||
referrerId,
|
||||
},
|
||||
})
|
||||
|
||||
// Create wallet for members
|
||||
if (role === 'MEMBER') {
|
||||
await prisma.wallet.create({
|
||||
data: {
|
||||
userId: user.id,
|
||||
balance: 0,
|
||||
totalEarnings: 0,
|
||||
totalWithdrawn: 0,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
return NextResponse.json(
|
||||
{ message: 'User created successfully', userId: user.id },
|
||||
{ status: 201 }
|
||||
)
|
||||
} catch (error) {
|
||||
console.error('Signup error:', error)
|
||||
return NextResponse.json(
|
||||
{ error: 'Internal server error' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user