47 lines
1.5 KiB
TypeScript
47 lines
1.5 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import { getServerSession } from 'next-auth'
|
|
import { authOptions } from '@/lib/auth'
|
|
import { connectToDatabase } from '@/lib/mongodb'
|
|
import { ObjectId } from 'mongodb'
|
|
|
|
export async function POST(
|
|
request: NextRequest,
|
|
{ params }: { params: Promise<{ id: string }> }
|
|
) {
|
|
try {
|
|
const { id } = await params
|
|
const session = await getServerSession(authOptions)
|
|
|
|
if (!session?.user?.id) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
}
|
|
|
|
const { db } = await connectToDatabase();
|
|
|
|
// ! check if the user is actually verified in the DB (never trust client!)
|
|
const user = await db.collection('users').findOne({ _id: new ObjectId(session.user.id) })
|
|
|
|
if (!user || !user.verified) {
|
|
return NextResponse.json({ error: 'Only verified users can verify coins... nga...' }, { status: 403 })
|
|
}
|
|
|
|
const coinId = id
|
|
const coin = await db.collection('coins').findOne({ _id: new ObjectId(coinId) })
|
|
|
|
if (!coin) {
|
|
return NextResponse.json({ error: 'Coin not found' }, { status: 404 })
|
|
}
|
|
|
|
const newStatus = !coin.verified //? toggle
|
|
await db.collection('coins').updateOne(
|
|
{ _id: new ObjectId(coinId) },
|
|
{ $set: { verified: newStatus } }
|
|
)
|
|
|
|
return NextResponse.json({ verified: newStatus })
|
|
|
|
} catch (error) {
|
|
console.error('Error verifying coin:', error)
|
|
return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 })
|
|
}
|
|
}
|