mirror of
https://github.com/ManInDark/HabitTrove.git
synced 2026-01-22 07:04:28 +01:00
support delete user (#139)
Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
This commit is contained in:
50
app/api/user/delete/route.ts
Normal file
50
app/api/user/delete/route.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import { NextResponse } from 'next/server'
|
||||
import { auth } from '@/auth'
|
||||
import { deleteUser } from '@/app/actions/data'
|
||||
import { getCurrentUser } from '@/lib/server-helpers'
|
||||
|
||||
export async function POST(req: Request) {
|
||||
try {
|
||||
const session = await auth()
|
||||
if (!session?.user?.id) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
}
|
||||
|
||||
const currentUserId = session.user.id
|
||||
const currentUser = await getCurrentUser()
|
||||
|
||||
if (!currentUser) {
|
||||
// This case should ideally not happen if session.user.id exists,
|
||||
// but as a safeguard:
|
||||
return NextResponse.json({ error: 'Unauthorized: User not found in system' }, { status: 401 })
|
||||
}
|
||||
|
||||
let userIdToDelete: string
|
||||
try {
|
||||
const body = await req.json()
|
||||
userIdToDelete = body.userId
|
||||
} catch (error) {
|
||||
return NextResponse.json({ error: 'Invalid request body: Could not parse JSON.' }, { status: 400 })
|
||||
}
|
||||
|
||||
|
||||
if (!userIdToDelete) {
|
||||
return NextResponse.json({ error: 'Bad Request: userId is required' }, { status: 400 })
|
||||
}
|
||||
|
||||
// Security Check: Users can only delete their own account unless they are an admin.
|
||||
if (!currentUser.isAdmin && userIdToDelete !== currentUserId) {
|
||||
return NextResponse.json({ error: 'Forbidden: You do not have permission to delete this user.' }, { status: 403 })
|
||||
}
|
||||
|
||||
await deleteUser(userIdToDelete)
|
||||
|
||||
return NextResponse.json({ message: 'User deleted successfully' }, { status: 200 })
|
||||
} catch (error) {
|
||||
console.error('Error deleting user:', error)
|
||||
if (error instanceof Error && error.message === 'User not found') {
|
||||
return NextResponse.json({ error: 'User not found' }, { status: 404 })
|
||||
}
|
||||
return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 })
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user