Download src/app/api/codes/enter/route.ts from HonzaH/Letschat: direct link, hf CLI and curl.
- Browser
- Download file 1.86 kB
-
https://huggingface.co/spaces/HonzaH/Letschat/resolve/main/src/app/api/codes/enter/route.ts
- Command line
-
hf download hf://spaces/HonzaH/Letschat/src/app/api/codes/enter/route.ts
-
curl -L -o route.ts https://huggingface.co/spaces/HonzaH/Letschat/resolve/main/src/app/api/codes/enter/route.ts
1.86 kB
| import { NextRequest, NextResponse } from 'next/server' | |
| import { isSupabaseConfigured } from 'lib/supabase/server' | |
| import { markCodeAsUsed } from 'lib/roomService' | |
| import { generateSessionHash, generateCode } from 'lib/codes' | |
| import { getCodeById, insertCode, updateCode } from 'lib/persistence' | |
| import { logEvent } from 'lib/logger' | |
| import { getAppSession } from 'lib/appSession' | |
| export async function POST(request: NextRequest) { | |
| try { | |
| const body = await request.json() | |
| const { codeId, userAgent } = body | |
| if (!isSupabaseConfigured()) { | |
| return NextResponse.json( | |
| { error: 'Supabase not configured (missing NEXT_PUBLIC_SUPABASE_URL / NEXT_PUBLIC_SUPABASE_ANON_KEY)' }, | |
| { status: 503 } | |
| ) | |
| } | |
| const session = await getAppSession() | |
| const detectedUserId = session?.userId | |
| const codeData = await getCodeById(codeId) | |
| if (!codeData) { | |
| return NextResponse.json({ error: 'Kód nenalezen' }, { status: 404 }) | |
| } | |
| let roomId = codeData.room_id | |
| if (!roomId) { | |
| return NextResponse.json({ error: 'Místnost neexistuje' }, { status: 404 }) | |
| } | |
| if (codeData.used !== 1) { | |
| const sessionHash = detectedUserId ? undefined : generateSessionHash(codeData.code, userAgent || '') | |
| await markCodeAsUsed(codeData.id, detectedUserId, sessionHash) | |
| } | |
| await logEvent({ | |
| module: 'codes', | |
| operation: 'enter', | |
| data: { codeId: codeData.id, roomId }, | |
| ip: (request as any).headers?.get?.('x-forwarded-for') || null, | |
| userAgent: (request as any).headers?.get?.('user-agent') || null | |
| }) | |
| return NextResponse.json({ | |
| roomId, | |
| codeId: codeData.id, | |
| success: true | |
| }) | |
| } catch (error) { | |
| console.error('Code entry error:', error) | |
| return NextResponse.json({ error: 'Vnitřní chyba serveru' }, { status: 500 }) | |
| } | |
| } | |