50 lines
1.3 KiB
TypeScript
50 lines
1.3 KiB
TypeScript
import { NextResponse } from "next/server"
|
|
import { prisma } from "@/lib/db"
|
|
import { randomUUID } from "crypto"
|
|
|
|
export async function POST(request: Request) {
|
|
try {
|
|
const { deviceFingerprint, pairingCode: incomingCode } = await request.json()
|
|
|
|
if (incomingCode) {
|
|
// Step 2: Parent's device confirms pairing
|
|
const device = await prisma.device.findFirst({
|
|
where: { deviceFingerprint: incomingCode },
|
|
include: { child: true },
|
|
})
|
|
|
|
if (!device) {
|
|
return NextResponse.json({ error: "Código inválido" }, { status: 404 })
|
|
}
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
childId: device.childId,
|
|
childName: device.child.name,
|
|
})
|
|
}
|
|
|
|
// Step 1: Child device requests pairing
|
|
const existingDevice = await prisma.device.findFirst({
|
|
where: { deviceFingerprint },
|
|
})
|
|
|
|
if (existingDevice) {
|
|
return NextResponse.json({
|
|
paired: true,
|
|
childId: existingDevice.childId,
|
|
})
|
|
}
|
|
|
|
const pairingCode = randomUUID().slice(0, 8)
|
|
|
|
return NextResponse.json({
|
|
paired: false,
|
|
pairingCode,
|
|
})
|
|
} catch (error) {
|
|
console.error("Pairing error:", error)
|
|
return NextResponse.json({ error: "Internal error" }, { status: 500 })
|
|
}
|
|
}
|