Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.
feat: add verification link
Marc Seitz82aa4594c646
diff --git a/app/(auth)/verify/page.tsx b/app/(auth)/verify/page.tsxindex 8c2378d0..7576d8fa 100644--- a/app/(auth)/verify/page.tsx+++ b/app/(auth)/verify/page.tsx@@ -66,14 +66,15 @@ export default async function VerifyPage({}) {const { token, verification_url, checksum } = searchParams;- let callbackUrl: string | null = null;+ let verifyUrl: string | null = null;let isExpired = false;// New flow: token-based verification (stored in Redis)if (token) {const magicLinkData = await getMagicLinkData(token);if (magicLinkData) {- callbackUrl = magicLinkData.callbackUrl;+ // Use the API route that will delete the token and redirect+ verifyUrl = `/api/verify/login-link?token=${token}`;} else {// Token not found or expired (Redis TTL handles expiration automatically)isExpired = true;@@ -82,12 +83,12 @@ export default async function VerifyPage({// Legacy flow: verification_url with checksum (backward compatibility)else if (verification_url && checksum) {if (isValidVerificationUrl(verification_url, checksum)) {- callbackUrl = verification_url;+ verifyUrl = verification_url;}}- // If no valid callback URL found, show appropriate error- if (!callbackUrl) {+ // If no valid verify URL found, show appropriate error+ if (!verifyUrl) {if (isExpired) {return (<div className="flex h-screen w-full flex-wrap">@@ -195,7 +196,7 @@ export default async function VerifyPage({</div><div className="flex flex-col gap-4 px-4 pt-8 sm:px-12"><div className="relative">- <Link href={callbackUrl}>+ <Link href={verifyUrl}><Button className="focus:shadow-outline w-full transform rounded bg-gray-800 px-4 py-2 text-white transition-colors duration-300 ease-in-out hover:bg-gray-900 focus:outline-none">Verify email</Button>diff --git a/app/api/verify/login-link/route.ts b/app/api/verify/login-link/route.tsnew file mode 100644index 00000000..a732b2d3--- /dev/null+++ b/app/api/verify/login-link/route.ts@@ -0,0 +1,36 @@+import { NextRequest, NextResponse } from "next/server";++import {+ deleteMagicLinkData,+ getMagicLinkData,+} from "@/lib/emails/send-verification-request";++export async function GET(request: NextRequest) {+ const searchParams = request.nextUrl.searchParams;+ const token = searchParams.get("token");++ if (!token) {+ return NextResponse.redirect(new URL("/login?error=InvalidToken", request.url));+ }++ try {+ // Get the magic link data from Redis+ const magicLinkData = await getMagicLinkData(token);++ if (!magicLinkData) {+ // Token not found or expired - redirect back to verify page to show error+ return NextResponse.redirect(new URL(`/verify?token=${token}`, request.url));+ }++ const { callbackUrl } = magicLinkData;++ // Delete the token from Redis to prevent reuse+ await deleteMagicLinkData(token);++ // Redirect to the callback URL (NextAuth verification URL)+ return NextResponse.redirect(callbackUrl);+ } catch (error) {+ console.error("Error verifying magic link:", error);+ return NextResponse.redirect(new URL("/login?error=VerificationError", request.url));+ }+}diff --git a/components/folders/edit-folder-modal.tsx b/components/folders/edit-folder-modal.tsxindex 042b8ed9..959d9d15 100644--- a/components/folders/edit-folder-modal.tsx+++ b/components/folders/edit-folder-modal.tsx@@ -131,4 +131,4 @@ export function EditFolderModal({</DialogContent></Dialog>);-}\ No newline at end of file+}diff --git a/lib/emails/send-verification-request.ts b/lib/emails/send-verification-request.tsindex 2d1bcbdd..0e9a2afc 100644--- a/lib/emails/send-verification-request.ts+++ b/lib/emails/send-verification-request.ts@@ -90,3 +90,17 @@ export const getMagicLinkData = async (return null;}};++/**+ * Delete magic link data from Redis after successful use+ * This prevents the link from being reused+ */+export const deleteMagicLinkData = async (token: string): Promise<boolean> => {+ try {+ const result = await redis.del(`${MAGIC_LINK_PREFIX}${token}`);+ return result === 1;+ } catch (error) {+ console.error("Error deleting magic link data:", error);+ return false;+ }+};