import { NextResponse } from "next/server"; import { prisma } from "@/lib/prisma"; import { hashVerificationToken } from "@/lib/email-verification"; import { getBaseUrl } from "@/lib/url"; export async function GET(request: Request) { const token = new URL(request.url).searchParams.get("token"); const baseUrl = getBaseUrl(); const fail = (message: string) => NextResponse.redirect(`${baseUrl}/konto?error=${encodeURIComponent(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 NextResponse.redirect( `${baseUrl}/konto?message=${encodeURIComponent("Deine E-Mail-Adresse wurde bestätigt.")}` ); }