diff --git a/src/app/verify/[token]/page.tsx b/src/app/verify/[token]/page.tsx new file mode 100644 index 00000000..7ba7251e --- /dev/null +++ b/src/app/verify/[token]/page.tsx @@ -0,0 +1,44 @@ +import { redirect } from "next/navigation"; +import { db } from "@/app/db/db"; +import { verificationTokens } from "@/app/db/schema/users"; +import { eq } from "drizzle-orm"; +import crypto from "crypto"; + +async function getEmailByToken(token: string) { + const secret = process.env.NEXTAUTH_SECRET!; + + const hashedToken = crypto + .createHash("sha256") + .update(token + secret) + .digest("hex"); + + const record = await db + .select() + .from(verificationTokens) + .where(eq(verificationTokens.token, hashedToken)) + .limit(1); + + if (!record.length) { + return null; + } + + return record[0].identifier; +} + +export default async function LoginPage({ + params, +}: { + params: Promise<{ token: string }>; +}) { + const { token } = await params; + + const email = await getEmailByToken(token); + + if (!email) { + redirect("/"); + } + + redirect( + `/api/auth/callback/email?token=${token}&email=${encodeURIComponent(email)}`, + ); +}