"use client"; /** * Magic-link landing page. * Briven emails: {appOrigin}/auth/verify?preAuthSessionId=…&linkCode=…&deviceId=… */ import Link from "next/link"; import { useEffect, useState } from "react"; import { consumeMagicLink } from "@/lib/magic-link"; export default function MagicLinkVerifyPage() { const [message, setMessage] = useState("Signing you in…"); const [failed, setFailed] = useState(false); useEffect(() => { let cancelled = false; async function run() { const params = new URLSearchParams(window.location.search); const preAuthSessionId = params.get("preAuthSessionId") || ""; const linkCode = params.get("linkCode") || ""; const deviceId = params.get("deviceId") || ""; if (!preAuthSessionId || !linkCode || !deviceId) { if (!cancelled) { setFailed(true); setMessage( "This sign-in link is incomplete. Go back to sign in and request a new magic link.", ); } return; } const res = await consumeMagicLink({ preAuthSessionId, deviceId, linkCode, }); if (cancelled) return; if (res.ok) { setMessage("Signed in. Taking you to your dashboard…"); window.dispatchEvent(new Event("krypco-auth-change")); const next = sessionStorage.getItem("krypco_magic_redirect") || params.get("next") || "/portal/dashboard"; sessionStorage.removeItem("krypco_magic_redirect"); const safe = next.startsWith("/") && !next.startsWith("//") ? next : "/portal/dashboard"; window.location.replace(safe); return; } setFailed(true); setMessage( res.message || "This sign-in link is invalid or expired. Request a new one.", ); } void run(); return () => { cancelled = true; }; }, []); return (

{message}

{failed ? (

Back to sign in

) : null}
); }