Fix authentication state persistence and admin role display
- Implement complete authentication system with JWT token validation - Add auth provider with persistent login state across page refreshes - Create multilingual login/register forms with Material-UI components - Fix token validation using raw SQL queries to bypass Prisma sync issues - Add comprehensive error handling for expired/invalid tokens - Create profile and settings pages with full i18n support - Add proper user role management (admin/user) with database sync - Implement secure middleware with CSRF protection and auth checks - Add debug endpoints for troubleshooting authentication issues - Fix Zustand store persistence for authentication state 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -1,17 +1,38 @@
|
||||
import { NextResponse } from 'next/server'
|
||||
import { createUser, generateToken } from '@/lib/auth'
|
||||
import { prisma } from '@/lib/db'
|
||||
import { userRegistrationSchema } from '@/lib/validation'
|
||||
import { createUserRegistrationSchema } from '@/lib/validation'
|
||||
import { z } from 'zod'
|
||||
|
||||
export const runtime = 'nodejs'
|
||||
|
||||
function getErrorMessages(locale: string = 'ro') {
|
||||
const messages = {
|
||||
ro: {
|
||||
userExists: 'Utilizatorul există deja',
|
||||
serverError: 'Eroare de server',
|
||||
invalidInput: 'Date de intrare invalide'
|
||||
},
|
||||
en: {
|
||||
userExists: 'User already exists',
|
||||
serverError: 'Server error',
|
||||
invalidInput: 'Invalid input data'
|
||||
}
|
||||
}
|
||||
return messages[locale as keyof typeof messages] || messages.ro
|
||||
}
|
||||
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
const url = new URL(request.url)
|
||||
const locale = url.searchParams.get('locale') || 'ro'
|
||||
const messages = getErrorMessages(locale)
|
||||
|
||||
const body = await request.json()
|
||||
|
||||
// Validate input
|
||||
const result = userRegistrationSchema.safeParse(body)
|
||||
const registrationSchema = createUserRegistrationSchema(locale)
|
||||
const result = registrationSchema.safeParse(body)
|
||||
if (!result.success) {
|
||||
const errors = result.error.errors.map(err => err.message).join(', ')
|
||||
return NextResponse.json({ error: errors }, { status: 400 })
|
||||
@@ -22,7 +43,7 @@ export async function POST(request: Request) {
|
||||
// Check if user exists
|
||||
const existing = await prisma.user.findUnique({ where: { email } })
|
||||
if (existing) {
|
||||
return NextResponse.json({ error: 'Utilizatorul există deja' }, { status: 409 })
|
||||
return NextResponse.json({ error: messages.userExists }, { status: 409 })
|
||||
}
|
||||
|
||||
// Create user
|
||||
@@ -39,14 +60,18 @@ export async function POST(request: Request) {
|
||||
})
|
||||
|
||||
return NextResponse.json({
|
||||
user: { id: user.id, email: user.email, name: user.name },
|
||||
user: { id: user.id, email: user.email, name: user.name, role: user.role, theme: user.theme, fontSize: user.fontSize },
|
||||
token
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('Registration error:', error)
|
||||
const url = new URL(request.url)
|
||||
const locale = url.searchParams.get('locale') || 'ro'
|
||||
const messages = getErrorMessages(locale)
|
||||
|
||||
if (error instanceof z.ZodError) {
|
||||
return NextResponse.json({ error: 'Date de intrare invalide' }, { status: 400 })
|
||||
return NextResponse.json({ error: messages.invalidInput }, { status: 400 })
|
||||
}
|
||||
return NextResponse.json({ error: 'Eroare de server' }, { status: 500 })
|
||||
return NextResponse.json({ error: messages.serverError }, { status: 500 })
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user