basic stracture and auth

in memory db :)
This commit is contained in:
dimitar committed 2025-11-07 20:59:56 +01:00
commit 3a554ba434
71 files changed
+28535

No files matched your search

@@ -0,0 +1,73 @@
import { NextRequest, NextResponse } from 'next/server'
import bcrypt from 'bcryptjs'
import { users, clients } from '../../../../lib/database'
export async function POST(request: NextRequest) {
try {
const { email, password, firstName, lastName, phone } = await request.json()
if (!email || !password || !firstName || !lastName) {
return NextResponse.json(
{ error: 'Missing required fields' },
{ status: 400 }
)
}
const existingUser = users.find(u => u.email === email)
if (existingUser) {
return NextResponse.json(
{ error: 'User already exists' },
{ status: 409 }
)
}
const hashedPassword = await bcrypt.hash(password, 10)
const userId = Math.random().toString(36).substr(2, 9)
const newUser = {
id: userId,
email,
firstName,
lastName,
password: hashedPassword,
phone,
role: 'client',
createdAt: new Date(),
updatedAt: new Date(),
}
users.push(newUser)
const newClient = {
id: Math.random().toString(36).substr(2, 9),
userId,
membershipType: 'basic',
membershipStatus: 'active',
joinDate: new Date(),
}
clients.push(newClient)
const { password: _, ...userWithoutPassword } = newUser
return NextResponse.json(
{
message: 'User registered successfully',
user: { ...userWithoutPassword, client: newClient }
},
{ status: 201 }
)
} catch (error) {
console.error('Registration error:', error)
return NextResponse.json(
{ error: 'Internal server error' },
{ status: 500 }
)
}
}
export async function GET() {
const usersWithoutPassword = users.map(({ password: _, ...user }) => user)
return NextResponse.json({ users: usersWithoutPassword })
}