Files
backerup-website/frontend/app/api/admin/users/route.ts
T
Anders Böttcher 0d58fafe4e feat: implement user authentication and admin management
- Add user management API endpoints for listing and creating users.
- Implement login and logout functionality with rate limiting.
- Create UI components for login form, logout button, and animated elements.
- Add session management with JWT and secure cookie handling.
- Seed initial admin user if no users exist.
- Introduce utility functions for password hashing and user authentication.
- Set up proxy middleware for route protection and security headers.
- Create a JSON file for user data storage.
- Add links configuration for external resources.
2026-05-12 14:58:11 +02:00

42 lines
1.4 KiB
TypeScript

import { NextResponse } from 'next/server'
import { listUsers, createUser } from '@/lib/users'
import { getSession } from '@/lib/session'
export async function GET() {
const session = await getSession()
if (!session || session.role !== 'admin') {
return NextResponse.json({ error: 'Forbidden' }, { status: 403 })
}
const users = await listUsers()
return NextResponse.json(users)
}
export async function POST(request: Request) {
const session = await getSession()
if (!session || session.role !== 'admin') {
return NextResponse.json({ error: 'Forbidden' }, { status: 403 })
}
let body: { username?: string; password?: string; role?: string }
try {
body = (await request.json()) as { username?: string; password?: string; role?: string }
} catch {
return NextResponse.json({ error: 'Invalid JSON' }, { status: 400 })
}
const username = String(body.username ?? '').trim()
const password = String(body.password ?? '')
const role = body.role === 'admin' ? 'admin' : ('user' as const)
if (!username || !password) {
return NextResponse.json({ error: 'Username and password are required' }, { status: 400 })
}
try {
const user = await createUser(username, password, role)
return NextResponse.json(user, { status: 201 })
} catch (e) {
return NextResponse.json({ error: (e as Error).message }, { status: 409 })
}
}