Skip to content

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

feat(view): prefill passcode from link URL (with d=1 lock)

Marc Seitz3723b84aa565
diff --git a/components/view/access-form/index.tsx b/components/view/access-form/index.tsx
index 45f29116..bc6143bb 100644
--- a/components/view/access-form/index.tsx
+++ b/components/view/access-form/index.tsx
@@ -35,6 +35,7 @@ export type DEFAULT_ACCESS_FORM_TYPE = {
export default function AccessForm({
data,
email,
+ password,
brand,
setData,
onSubmitHandler,
@@ -50,6 +51,7 @@ export default function AccessForm({
isLoading,
linkId,
disableEditEmail,
+ disableEditPassword,
useCustomAccessForm,
customFields,
logoOnAccessForm,
@@ -57,6 +59,7 @@ export default function AccessForm({
}: {
data: DEFAULT_ACCESS_FORM_TYPE;
email: string | null | undefined;
+ password?: string | null | undefined;
setData: React.Dispatch<React.SetStateAction<DEFAULT_ACCESS_FORM_TYPE>>;
onSubmitHandler: React.FormEventHandler<HTMLFormElement>;
brand?: Partial<Brand> | Partial<DataroomBrand> | null;
@@ -72,6 +75,7 @@ export default function AccessForm({
isLoading: boolean;
linkId?: string;
disableEditEmail?: boolean;
+ disableEditPassword?: boolean;
useCustomAccessForm?: boolean;
customFields?: Partial<CustomField>[];
logoOnAccessForm?: boolean;
@@ -92,6 +96,7 @@ export default function AccessForm({
);
const lockEmailField = Boolean(disableEditEmail) || isAgreementLocked;
const lockNameField = isAgreementLocked;
+ const lockPasswordField = Boolean(disableEditPassword);
useEffect(() => {
const userEmail = email;
@@ -103,6 +108,18 @@ export default function AccessForm({
}
}, [email, setData]);
+ // Prefill the passcode from a link param (e.g. ?passcode=...) so it can be
+ // shared alongside the email. Only seed when the field is still empty to
+ // preserve any edits the viewer makes.
+ useEffect(() => {
+ if (password) {
+ setData((prevData: DEFAULT_ACCESS_FORM_TYPE) => ({
+ ...prevData,
+ password: prevData.password ?? password,
+ }));
+ }
+ }, [password, setData]);
+
const isFormValid = () => {
if (requireEmail) {
if (!data.email || !isEmailValid) return false;
@@ -198,7 +215,10 @@ export default function AccessForm({
/>
) : null}
{requirePassword ? (
- <PasswordSection {...{ data, setData, brand }} />
+ <PasswordSection
+ {...{ data, setData, brand }}
+ disableEditPassword={lockPasswordField}
+ />
) : null}
{customFields?.length ? (
<CustomFieldsSection
diff --git a/components/view/access-form/password-section.tsx b/components/view/access-form/password-section.tsx
index 4ce53bbb..b96330d9 100644
--- a/components/view/access-form/password-section.tsx
+++ b/components/view/access-form/password-section.tsx
@@ -14,10 +14,12 @@ export default function PasswordSection({
data,
setData,
brand,
+ disableEditPassword,
}: {
data: DEFAULT_ACCESS_FORM_TYPE;
setData: Dispatch<SetStateAction<DEFAULT_ACCESS_FORM_TYPE>>;
brand?: Partial<Brand> | Partial<DataroomBrand> | null;
+ disableEditPassword?: boolean;
}) {
const { password } = data;
const theme = useAccessFormTheme();
@@ -47,13 +49,14 @@ export default function PasswordSection({
borderColor: theme.controlBorderColor,
"--access-placeholder": theme.controlPlaceholderColor,
"--access-input-focus": theme.controlBorderStrongColor,
- color: theme.textColor,
+ color: disableEditPassword ? theme.subtleTextColor : theme.textColor,
} as CSSProperties}
value={password || ""}
placeholder={t("fields.password.placeholder", "Enter passcode")}
onChange={(e) => {
setData({ ...data, password: e.target.value });
}}
+ disabled={disableEditPassword}
aria-invalid="true"
data-1p-ignore
/>
diff --git a/components/view/dataroom/dataroom-document-view.tsx b/components/view/dataroom/dataroom-document-view.tsx
index fb297377..236ec9d5 100644
--- a/components/view/dataroom/dataroom-document-view.tsx
+++ b/components/view/dataroom/dataroom-document-view.tsx
@@ -83,6 +83,8 @@ export default function DataroomDocumentView({
useAdvancedExcelViewer,
previewToken,
disableEditEmail,
+ urlPasscode,
+ disableEditPassword,
useCustomAccessForm,
isEmbedded,
preview,
@@ -104,6 +106,8 @@ export default function DataroomDocumentView({
useAdvancedExcelViewer?: boolean;
previewToken?: string;
disableEditEmail?: boolean;
+ urlPasscode?: string;
+ disableEditPassword?: boolean;
useCustomAccessForm?: boolean;
isEmbedded?: boolean;
preview?: boolean;
@@ -158,6 +162,7 @@ export default function DataroomDocumentView({
body: JSON.stringify({
...data,
email: data.email ?? verifiedEmail ?? userEmail ?? null,
+ password: data.password ?? urlPasscode ?? undefined,
linkId: link.id,
documentId: link.dataroomDocument.document.id,
documentName: link.dataroomDocument.document.name,
@@ -390,6 +395,7 @@ export default function DataroomDocumentView({
<AccessForm
data={data}
email={userEmail}
+ password={urlPasscode}
setData={setData}
onSubmitHandler={handleSubmit}
requireEmail={emailProtected}
@@ -404,6 +410,7 @@ export default function DataroomDocumentView({
isLoading={isLoading}
linkId={link.id}
disableEditEmail={disableEditEmail}
+ disableEditPassword={disableEditPassword}
useCustomAccessForm={useCustomAccessForm}
brand={brand}
customFields={link.customFields}
diff --git a/components/view/dataroom/dataroom-view.tsx b/components/view/dataroom/dataroom-view.tsx
index 909b2978..f9af4a39 100644
--- a/components/view/dataroom/dataroom-view.tsx
+++ b/components/view/dataroom/dataroom-view.tsx
@@ -67,6 +67,8 @@ export default function DataroomView({
verifiedEmail,
previewToken,
disableEditEmail,
+ urlPasscode,
+ disableEditPassword,
useCustomAccessForm,
logoOnAccessForm,
isEmbedded,
@@ -83,6 +85,8 @@ export default function DataroomView({
verifiedEmail?: string;
previewToken?: string;
disableEditEmail?: boolean;
+ urlPasscode?: string;
+ disableEditPassword?: boolean;
useCustomAccessForm?: boolean;
isEmbedded?: boolean;
preview?: boolean;
@@ -149,6 +153,7 @@ export default function DataroomView({
body: JSON.stringify({
...data,
email: data.email ?? verifiedEmail ?? userEmail ?? null,
+ password: data.password ?? urlPasscode ?? undefined,
linkId: link.id,
userId: userId ?? null,
dataroomId: dataroom?.id,
@@ -287,6 +292,7 @@ export default function DataroomView({
<AccessForm
data={data}
email={userEmail}
+ password={urlPasscode}
setData={setData}
onSubmitHandler={handleSubmit}
requireEmail={emailProtected}
@@ -301,6 +307,7 @@ export default function DataroomView({
isLoading={isLoading}
linkId={link.id}
disableEditEmail={disableEditEmail}
+ disableEditPassword={disableEditPassword}
useCustomAccessForm={useCustomAccessForm}
brand={brand}
customFields={link.customFields}
diff --git a/components/view/document-view.tsx b/components/view/document-view.tsx
index 930b4883..f5ba9069 100644
--- a/components/view/document-view.tsx
+++ b/components/view/document-view.tsx
@@ -69,6 +69,8 @@ export default function DocumentView({
useAdvancedExcelViewer,
previewToken,
disableEditEmail,
+ urlPasscode,
+ disableEditPassword,
useCustomAccessForm,
logoOnAccessForm,
isEmbedded,
@@ -92,6 +94,8 @@ export default function DocumentView({
useAdvancedExcelViewer?: boolean;
previewToken?: string;
disableEditEmail?: boolean;
+ urlPasscode?: string;
+ disableEditPassword?: boolean;
useCustomAccessForm?: boolean;
isEmbedded?: boolean;
logoOnAccessForm?: boolean;
@@ -136,6 +140,7 @@ export default function DocumentView({
body: JSON.stringify({
...data,
email: data.email ?? verifiedEmail ?? userEmail ?? null,
+ password: data.password ?? urlPasscode ?? undefined,
linkId: link.id,
documentId: document.id,
documentName: document.name,
@@ -280,6 +285,7 @@ export default function DocumentView({
<AccessForm
data={data}
email={userEmail}
+ password={urlPasscode}
setData={setData}
onSubmitHandler={handleSubmit}
requireEmail={emailProtected}
@@ -295,6 +301,7 @@ export default function DocumentView({
brand={brand}
linkId={link.id}
disableEditEmail={disableEditEmail}
+ disableEditPassword={disableEditPassword}
useCustomAccessForm={useCustomAccessForm}
customFields={link.customFields}
logoOnAccessForm={logoOnAccessForm}
diff --git a/lib/hooks/use-url-passcode.ts b/lib/hooks/use-url-passcode.ts
new file mode 100644
index 00000000..dd742dce
--- /dev/null
+++ b/lib/hooks/use-url-passcode.ts
@@ -0,0 +1,40 @@
+import { useRouter } from "next/router";
+
+import { useEffect, useState } from "react";
+
+// A link's passcode is a secret, so it is passed in the URL *fragment*
+// (e.g. `#passcode=abc123`) instead of the query string. Fragments are never
+// sent to the server, so the passcode stays out of access logs and `Referer`
+// headers. This has to run on the client: `router.query` (and the server)
+// never see the fragment.
+function readPasscodeFromHash(): string | undefined {
+ // `window` is undefined during SSR — the fragment only exists on the client.
+ // Returning undefined keeps the server render (a loading spinner) and the
+ // initial client render in sync.
+ if (typeof window === "undefined") return undefined;
+ const hash = window.location.hash.replace(/^#/, "");
+ if (!hash) return undefined;
+ const value = new URLSearchParams(hash).get("passcode");
+ return value ? value : undefined;
+}
+
+export function useUrlPasscode(): string | undefined {
+ const router = useRouter();
+ // Resolve the fragment synchronously on the first client render so a field
+ // that should be locked (d=1) never briefly renders as unlocked/editable
+ // before an effect could populate it.
+ const [passcode, setPasscode] = useState<string | undefined>(
+ readPasscodeFromHash,
+ );
+
+ useEffect(() => {
+ // Re-read on client-side navigations (router path change) and manual
+ // fragment edits (hashchange). Identical values are a no-op in React.
+ setPasscode(readPasscodeFromHash());
+ const handleHashChange = () => setPasscode(readPasscodeFromHash());
+ window.addEventListener("hashchange", handleHashChange);
+ return () => window.removeEventListener("hashchange", handleHashChange);
+ }, [router.asPath]);
+
+ return passcode;
+}
diff --git a/pages/view/[linkId]/d/[documentId].tsx b/pages/view/[linkId]/d/[documentId].tsx
index 0c17d33a..7c478d33 100644
--- a/pages/view/[linkId]/d/[documentId].tsx
+++ b/pages/view/[linkId]/d/[documentId].tsx
@@ -13,6 +13,7 @@ import z from "zod";
import { fetchLinkDataById } from "@/lib/api/links/link-data";
import { getFeatureFlags } from "@/lib/featureFlags";
+import { useUrlPasscode } from "@/lib/hooks/use-url-passcode";
import {
buildViewerI18nPageProps,
type ViewerI18nPageProps,
@@ -78,6 +79,7 @@ function DataroomDocumentViewPageInner({
const { data: session, status } = useSession();
const [storedToken, setStoredToken] = useState<string | undefined>(undefined);
const [storedEmail, setStoredEmail] = useState<string | undefined>(undefined);
+ const urlPasscode = useUrlPasscode();
useEffect(() => {
// Retrieve token from cookie on component mount
@@ -122,6 +124,7 @@ function DataroomDocumentViewPageInner({
previewToken?: string;
preview?: string;
};
+ const disableEditPassword = !!disableEditEmail && !!urlPasscode;
const { link, brand } = linkData;
// Render the document view for DATAROOM_LINK
@@ -194,6 +197,8 @@ function DataroomDocumentViewPageInner({
useAdvancedExcelViewer={useAdvancedExcelViewer}
previewToken={previewToken}
disableEditEmail={!!disableEditEmail}
+ urlPasscode={urlPasscode}
+ disableEditPassword={disableEditPassword}
useCustomAccessForm={useCustomAccessForm}
logoOnAccessForm={logoOnAccessForm}
token={storedToken}
diff --git a/pages/view/[linkId]/embed.tsx b/pages/view/[linkId]/embed.tsx
index 9beb53ff..5ae48581 100644
--- a/pages/view/[linkId]/embed.tsx
+++ b/pages/view/[linkId]/embed.tsx
@@ -5,6 +5,7 @@ import { useEffect, useState } from "react";
import NotFound from "@/pages/404";
import { useAnalytics } from "@/lib/analytics";
+import { useUrlPasscode } from "@/lib/hooks/use-url-passcode";
import LoadingSpinner from "@/components/ui/loading-spinner";
import DataroomView from "@/components/view/dataroom/dataroom-view";
@@ -20,6 +21,7 @@ function EmbedPageInner(props: ViewPageProps) {
const router = useRouter();
const [isEmbedded, setIsEmbedded] = useState<boolean | null>(null);
const analytics = useAnalytics();
+ const urlPasscode = useUrlPasscode();
useEffect(() => {
// Only run when router is ready and linkId is present
@@ -72,6 +74,7 @@ function EmbedPageInner(props: ViewPageProps) {
d: string;
previewToken?: string;
};
+ const disableEditPassword = !!disableEditEmail && !!urlPasscode;
const { linkType, brand } = props.linkData;
// Render the document view for DOCUMENT_LINK
@@ -121,6 +124,8 @@ function EmbedPageInner(props: ViewPageProps) {
useAdvancedExcelViewer={props.useAdvancedExcelViewer}
previewToken={previewToken}
disableEditEmail={!!disableEditEmail}
+ urlPasscode={urlPasscode}
+ disableEditPassword={disableEditPassword}
useCustomAccessForm={props.useCustomAccessForm}
verifiedEmail={verifiedEmail}
isEmbedded
@@ -172,6 +177,8 @@ function EmbedPageInner(props: ViewPageProps) {
brand={brand}
previewToken={previewToken}
disableEditEmail={!!disableEditEmail}
+ urlPasscode={urlPasscode}
+ disableEditPassword={disableEditPassword}
useCustomAccessForm={props.useCustomAccessForm}
verifiedEmail={verifiedEmail}
isEmbedded
diff --git a/pages/view/[linkId]/index.tsx b/pages/view/[linkId]/index.tsx
index c6a719db..c1576254 100644
--- a/pages/view/[linkId]/index.tsx
+++ b/pages/view/[linkId]/index.tsx
@@ -14,6 +14,7 @@ import z from "zod";
import { fetchLinkDataById } from "@/lib/api/links/link-data";
import { getFeatureFlags } from "@/lib/featureFlags";
+import { useUrlPasscode } from "@/lib/hooks/use-url-passcode";
import {
buildViewerI18nPageProps,
type ViewerI18nPageProps,
@@ -371,6 +372,7 @@ function ViewPageInner({
const { data: session, status } = useSession();
const [storedToken, setStoredToken] = useState<string | undefined>(undefined);
const [storedEmail, setStoredEmail] = useState<string | undefined>(undefined);
+ const urlPasscode = useUrlPasscode();
useEffect(() => {
// Retrieve token from cookie on component mount
@@ -423,6 +425,7 @@ function ViewPageInner({
previewToken?: string;
preview?: string;
};
+ const disableEditPassword = !!disableEditEmail && !!urlPasscode;
const { linkType } = linkData;
// Render workflow access view for WORKFLOW_LINK
@@ -517,6 +520,8 @@ function ViewPageInner({
useAdvancedExcelViewer={useAdvancedExcelViewer}
previewToken={previewToken}
disableEditEmail={!!disableEditEmail}
+ urlPasscode={urlPasscode}
+ disableEditPassword={disableEditPassword}
useCustomAccessForm={useCustomAccessForm}
logoOnAccessForm={logoOnAccessForm}
token={storedToken}
@@ -597,6 +602,8 @@ function ViewPageInner({
isProtected={!!(emailProtected || linkPassword || enableAgreement)}
brand={brand}
disableEditEmail={!!disableEditEmail}
+ urlPasscode={urlPasscode}
+ disableEditPassword={disableEditPassword}
useCustomAccessForm={useCustomAccessForm}
logoOnAccessForm={logoOnAccessForm}
token={storedToken}
diff --git a/pages/view/domains/[domain]/[slug]/d/[documentId].tsx b/pages/view/domains/[domain]/[slug]/d/[documentId].tsx
index d6b14872..8a1aa0e8 100644
--- a/pages/view/domains/[domain]/[slug]/d/[documentId].tsx
+++ b/pages/view/domains/[domain]/[slug]/d/[documentId].tsx
@@ -13,6 +13,7 @@ import z from "zod";
import { fetchLinkDataByDomainSlug } from "@/lib/api/links/link-data";
import { getFeatureFlags } from "@/lib/featureFlags";
+import { useUrlPasscode } from "@/lib/hooks/use-url-passcode";
import {
buildViewerI18nPageProps,
type ViewerI18nPageProps,
@@ -78,6 +79,7 @@ function DataroomDocumentViewPageInner({
const { data: session, status } = useSession();
const [storedToken, setStoredToken] = useState<string | undefined>(undefined);
const [storedEmail, setStoredEmail] = useState<string | undefined>(undefined);
+ const urlPasscode = useUrlPasscode();
useEffect(() => {
// Retrieve token from cookie on component mount
@@ -123,6 +125,7 @@ function DataroomDocumentViewPageInner({
previewToken?: string;
preview?: string;
};
+ const disableEditPassword = !!disableEditEmail && !!urlPasscode;
const { link, brand } = linkData;
// Render the document view for DATAROOM_LINK
@@ -195,6 +198,8 @@ function DataroomDocumentViewPageInner({
useAdvancedExcelViewer={useAdvancedExcelViewer}
previewToken={previewToken}
disableEditEmail={!!disableEditEmail}
+ urlPasscode={urlPasscode}
+ disableEditPassword={disableEditPassword}
useCustomAccessForm={useCustomAccessForm}
token={storedToken}
verifiedEmail={verifiedEmail}
diff --git a/pages/view/domains/[domain]/[slug]/embed.tsx b/pages/view/domains/[domain]/[slug]/embed.tsx
index 93957a8c..b0427809 100644
--- a/pages/view/domains/[domain]/[slug]/embed.tsx
+++ b/pages/view/domains/[domain]/[slug]/embed.tsx
@@ -6,6 +6,7 @@ import NotFound from "@/pages/404";
import { Brand, DataroomBrand } from "@prisma/client";
import { useAnalytics } from "@/lib/analytics";
+import { useUrlPasscode } from "@/lib/hooks/use-url-passcode";
import { type ViewerI18nPageProps } from "@/lib/i18n/viewer-page-props";
import { LinkWithDataroom, LinkWithDocument } from "@/lib/types";
@@ -49,6 +50,7 @@ function EmbedPageInner(props: DomainEmbedPageProps) {
const router = useRouter();
const [isEmbedded, setIsEmbedded] = useState<boolean | null>(null);
const analytics = useAnalytics();
+ const urlPasscode = useUrlPasscode();
useEffect(() => {
if (!router.isReady || !router.query.slug) return;
@@ -109,6 +111,7 @@ function EmbedPageInner(props: DomainEmbedPageProps) {
d: string;
previewToken?: string;
};
+ const disableEditPassword = !!disableEditEmail && !!urlPasscode;
const { linkType } = props.linkData;
@@ -155,6 +158,8 @@ function EmbedPageInner(props: DomainEmbedPageProps) {
useAdvancedExcelViewer={props.useAdvancedExcelViewer}
previewToken={previewToken}
disableEditEmail={!!disableEditEmail}
+ urlPasscode={urlPasscode}
+ disableEditPassword={disableEditPassword}
useCustomAccessForm={props.useCustomAccessForm}
logoOnAccessForm={props.logoOnAccessForm}
verifiedEmail={verifiedEmail}
@@ -205,6 +210,8 @@ function EmbedPageInner(props: DomainEmbedPageProps) {
brand={brand}
previewToken={previewToken}
disableEditEmail={!!disableEditEmail}
+ urlPasscode={urlPasscode}
+ disableEditPassword={disableEditPassword}
useCustomAccessForm={props.useCustomAccessForm}
logoOnAccessForm={props.logoOnAccessForm}
verifiedEmail={verifiedEmail}
diff --git a/pages/view/domains/[domain]/[slug]/index.tsx b/pages/view/domains/[domain]/[slug]/index.tsx
index 262ceb41..1e9b6e1c 100644
--- a/pages/view/domains/[domain]/[slug]/index.tsx
+++ b/pages/view/domains/[domain]/[slug]/index.tsx
@@ -14,6 +14,7 @@ import z from "zod";
import { fetchLinkDataByDomainSlug } from "@/lib/api/links/link-data";
import { getFeatureFlags } from "@/lib/featureFlags";
+import { useUrlPasscode } from "@/lib/hooks/use-url-passcode";
import {
buildViewerI18nPageProps,
type ViewerI18nPageProps,
@@ -362,6 +363,7 @@ function ViewPageInner({
const { data: session, status } = useSession();
const [storedToken, setStoredToken] = useState<string | undefined>(undefined);
const [storedEmail, setStoredEmail] = useState<string | undefined>(undefined);
+ const urlPasscode = useUrlPasscode();
useEffect(() => {
// Retrieve token from cookie on component mount
@@ -407,6 +409,7 @@ function ViewPageInner({
previewToken?: string;
preview?: string;
};
+ const disableEditPassword = !!disableEditEmail && !!urlPasscode;
const { linkType } = linkData;
// Render workflow access view for WORKFLOW_LINK
@@ -504,6 +507,8 @@ function ViewPageInner({
useAdvancedExcelViewer={useAdvancedExcelViewer}
previewToken={previewToken}
disableEditEmail={!!disableEditEmail}
+ urlPasscode={urlPasscode}
+ disableEditPassword={disableEditPassword}
useCustomAccessForm={useCustomAccessForm}
token={storedToken}
verifiedEmail={verifiedEmail}
@@ -582,6 +587,8 @@ function ViewPageInner({
isProtected={!!(emailProtected || linkPassword || enableAgreement)}
brand={brand}
disableEditEmail={!!disableEditEmail}
+ urlPasscode={urlPasscode}
+ disableEditPassword={disableEditPassword}
useCustomAccessForm={useCustomAccessForm}
token={storedToken}
verifiedEmail={verifiedEmail}