route.ts
919 bytes
| 1 | import { NextResponse } from 'next/server'; |
|---|---|
| 2 | import { currentUserId } from '@/auth'; |
| 3 | import { ensureShareToken, getDebateOwnership } from '@/db/repositories'; |
| 4 | |
| 5 | export const runtime = 'nodejs'; |
| 6 | |
| 7 | type Params = { params: Promise<{ id: string }> }; |
| 8 | |
| 9 | /** POST: mint (or return the existing) unlisted share token for a debate. */ |
| 10 | export async function POST(request: Request, { params }: Params) { |
| 11 | const { id } = await params; |
| 12 | const ownership = await getDebateOwnership(id); |
| 13 | if (!ownership) return NextResponse.json({ error: 'Not found' }, { status: 404 }); |
| 14 | |
| 15 | const userId = await currentUserId(); |
| 16 | const canShare = ownership.userId === null || ownership.userId === userId; |
| 17 | if (!canShare) return NextResponse.json({ error: 'Forbidden' }, { status: 403 }); |
| 18 | |
| 19 | const token = await ensureShareToken(id); |
| 20 | const origin = new URL(request.url).origin; |
| 21 | return NextResponse.json({ token, url: `${origin}/r/${token}` }); |
| 22 | } |
| 23 | |