import { prisma } from "@/lib/prisma"; import { hashVerificationToken } from "@/lib/email-verification"; import { withFlash } from "@/lib/flash"; import { redirectWithinSite } from "@/lib/redirect"; export async function GET(request: Request) { const token = new URL(request.url).searchParams.get("token"); const fail = (message: string) => withFlash(redirectWithinSite("/konto"), "error", message); if (!token) { return fail("Dieser Bestätigungslink ist ungültig."); } const verificationToken = await prisma.emailVerificationToken.findUnique({ where: { tokenHash: hashVerificationToken(token) }, }); const isValid = verificationToken && verificationToken.usedAt === null && verificationToken.expiresAt > new Date(); if (!isValid) { return fail("Dieser Bestätigungslink ist ungültig oder abgelaufen. Bitte fordere über /konto einen neuen an."); } await prisma.$transaction([ prisma.customer.update({ where: { id: verificationToken.customerId }, data: { emailVerifiedAt: new Date() }, }), prisma.emailVerificationToken.update({ where: { id: verificationToken.id }, data: { usedAt: new Date() }, }), ]); return withFlash( redirectWithinSite("/konto"), "success", "Deine E-Mail-Adresse wurde bestätigt. Du kannst jetzt bestellen." ); }