Skip to content

Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.

feat: add otp to downloads page

Marc Seitz9440ca4d20b6
diff --git a/components/view/dataroom/download-otp-verification.tsx b/components/view/dataroom/download-otp-verification.tsx
index 1c0f5a12..6472d32e 100644
--- a/components/view/dataroom/download-otp-verification.tsx
+++ b/components/view/dataroom/download-otp-verification.tsx
@@ -13,7 +13,8 @@ const REGEXP_ONLY_DIGITS = "^\\d+$";
export interface DownloadOtpVerificationProps {
linkId: string;
- viewId: string;
+ /** viewId is optional — when omitted the server resolves the view by email + linkId. */
+ viewId?: string;
email: string;
onVerified: () => void;
onCancel?: () => void;
@@ -44,11 +45,13 @@ export function DownloadOtpVerification({
setIsSending(true);
setError(null);
try {
+ const body: Record<string, string> = { linkId, email };
+ if (viewId) body.viewId = viewId;
const res = await fetch("/api/links/download/verify", {
method: "POST",
headers: { "Content-Type": "application/json" },
credentials: "include",
- body: JSON.stringify({ linkId, viewId, email }),
+ body: JSON.stringify(body),
});
const data = await res.json().catch(() => ({}));
if (!res.ok) {
@@ -73,11 +76,13 @@ export function DownloadOtpVerification({
setIsLoading(true);
setError(null);
try {
+ const body: Record<string, string> = { linkId, email, code: code! };
+ if (viewId) body.viewId = viewId;
const res = await fetch("/api/links/download/verify", {
method: "POST",
headers: { "Content-Type": "application/json" },
credentials: "include",
- body: JSON.stringify({ linkId, viewId, email, code }),
+ body: JSON.stringify(body),
});
const data = await res.json().catch(() => ({}));
if (!res.ok) {
diff --git a/components/view/dataroom/downloads-panel.tsx b/components/view/dataroom/downloads-panel.tsx
index c9a4318f..c78c8fb8 100644
--- a/components/view/dataroom/downloads-panel.tsx
+++ b/components/view/dataroom/downloads-panel.tsx
@@ -35,7 +35,6 @@ export function DownloadsPanel({ linkId }: { linkId: string }) {
const [loading, setLoading] = useState(true);
const [hasSession, setHasSession] = useState<boolean | null>(null);
const [email, setEmail] = useState("");
- const [viewId, setViewId] = useState<string | null>(null);
const [step, setStep] = useState<"session" | "email" | "otp" | "list">(
"session",
);
@@ -43,6 +42,7 @@ export function DownloadsPanel({ linkId }: { linkId: string }) {
const [jobsLoading, setJobsLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
const [expandedJobId, setExpandedJobId] = useState<string | null>(null);
+ const [emailSubmitting, setEmailSubmitting] = useState(false);
// ── Session check ──────────────────────────────────────────────────
const checkSession = useCallback(async () => {
@@ -137,13 +137,14 @@ export function DownloadsPanel({ linkId }: { linkId: string }) {
};
}, [step, jobs, fetchJobs]);
- // ── Email submit (POST — avoids leaking PII in URL) ────────────────
+ // ── Email submit — send OTP directly (view is resolved server-side) ─
const handleEmailSubmit = async (e: React.FormEvent) => {
e.preventDefault();
if (!linkId || !email.trim()) return;
setError(null);
+ setEmailSubmitting(true);
try {
- const res = await fetch(`/api/links/download/by-email`, {
+ const res = await fetch(`/api/links/download/verify`, {
method: "POST",
headers: { "Content-Type": "application/json" },
credentials: "include",
@@ -152,23 +153,27 @@ export function DownloadsPanel({ linkId }: { linkId: string }) {
email: email.trim(),
}),
});
- if (!res.ok) {
+ const data = await res.json().catch(() => ({}));
+ if (res.status === 404) {
setError(
"No access found for this email. Use the email you used to open the dataroom.",
);
return;
}
- const data = await res.json();
- if (!data.viewId) {
- setError(
- "No access found for this email. Use the email you used to open the dataroom.",
- );
+ if (res.status === 429) {
+ setError(data.error ?? "Too many requests. Please try again later.");
return;
}
- setViewId(data.viewId);
+ if (!res.ok) {
+ setError(data.error ?? "Something went wrong.");
+ return;
+ }
+ // OTP sent successfully — move to verification step
setStep("otp");
} catch {
setError("Something went wrong.");
+ } finally {
+ setEmailSubmitting(false);
}
};
@@ -180,8 +185,17 @@ export function DownloadsPanel({ linkId }: { linkId: string }) {
// ── Download helpers ───────────────────────────────────────────────
const handleDownload = (url: string) => {
+ // Ensure we use a relative path so the request goes to the current origin
+ // (where the session cookie lives), not a potentially different subdomain.
+ let href = url;
+ try {
+ const parsed = new URL(url, window.location.origin);
+ href = parsed.pathname + parsed.search;
+ } catch {
+ // url is already relative, use as-is
+ }
const a = document.createElement("a");
- a.href = url;
+ a.href = href;
a.rel = "noopener noreferrer";
document.body.appendChild(a);
a.click();
@@ -259,21 +273,20 @@ export function DownloadsPanel({ linkId }: { linkId: string }) {
required
/>
{error && <p className="text-sm text-destructive">{error}</p>}
- <Button type="submit" className="w-full">
- Continue
+ <Button type="submit" className="w-full" disabled={emailSubmitting}>
+ {emailSubmitting ? "Sending code..." : "Continue"}
</Button>
</form>
</div>
);
}
- if (step === "otp" && viewId) {
+ if (step === "otp") {
return (
<div className="mx-auto flex min-h-screen max-w-sm flex-col justify-center px-4">
<h1 className="text-xl font-semibold">Verify your email</h1>
<DownloadOtpVerification
linkId={linkId}
- viewId={viewId}
email={email.trim()}
onVerified={handleOtpVerified}
/>
diff --git a/pages/api/links/download/verify.ts b/pages/api/links/download/verify.ts
index bad5b6b7..dfb82fab 100644
--- a/pages/api/links/download/verify.ts
+++ b/pages/api/links/download/verify.ts
@@ -39,29 +39,46 @@ export default async function handler(
return res.status(405).end(`Method ${req.method} Not Allowed`);
}
- const { linkId, viewId, email, code } = req.body as {
+ const { linkId, viewId: providedViewId, email, code } = req.body as {
linkId?: string;
viewId?: string;
email?: string;
code?: string;
};
- if (!linkId || !viewId || !email) {
+ if (!linkId || !email) {
return res
.status(400)
- .json({ error: "linkId, viewId and email are required" });
+ .json({ error: "linkId and email are required" });
}
- const view = await prisma.view.findUnique({
- where: { id: viewId, linkId, viewType: "DATAROOM_VIEW" },
- select: {
- id: true,
- dataroomId: true,
- viewerId: true,
- viewerEmail: true,
- link: { select: { teamId: true } },
- },
- });
+ // Look up the view: use provided viewId if given, otherwise find by email + linkId
+ const view = providedViewId
+ ? await prisma.view.findUnique({
+ where: { id: providedViewId, linkId, viewType: "DATAROOM_VIEW" },
+ select: {
+ id: true,
+ dataroomId: true,
+ viewerId: true,
+ viewerEmail: true,
+ link: { select: { teamId: true } },
+ },
+ })
+ : await prisma.view.findFirst({
+ where: {
+ linkId,
+ viewType: "DATAROOM_VIEW",
+ viewerEmail: { equals: email, mode: "insensitive" },
+ },
+ select: {
+ id: true,
+ dataroomId: true,
+ viewerId: true,
+ viewerEmail: true,
+ link: { select: { teamId: true } },
+ },
+ orderBy: { viewedAt: "desc" },
+ });
if (!view || view.viewerEmail?.toLowerCase() !== email.toLowerCase()) {
return res
@@ -69,6 +86,9 @@ export default async function handler(
.json({ error: "View not found or email does not match" });
}
+ // Use the resolved viewId from here on
+ const viewId = view.id;
+
if (!view.link?.teamId) {
return res.status(400).json({ error: "Link has no team" });
}