Skip to content

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

feat: add verification flow to papermark and custom domains

Marc Seitze80549360546
diff --git a/components/view/document-view.tsx b/components/view/document-view.tsx
index 0f70b53b..9bd1896e 100644
--- a/components/view/document-view.tsx
+++ b/components/view/document-view.tsx
@@ -11,6 +11,7 @@ import { ExtendedRecordMap } from "notion-types";
import EmailVerificationMessage from "./email-verification-form";
import ViewData from "./view-data";
import { Brand } from "@prisma/client";
+import { useRouter } from "next/router";
export type DEFAULT_DOCUMENT_VIEW_TYPE = {
viewId: string;
@@ -23,28 +24,27 @@ export default function DocumentView({
userEmail,
userId,
isProtected,
- hasEmailVerification,
notionData,
+ brand,
token,
verifiedEmail,
- brand,
}: {
link: LinkWithDocument;
userEmail: string | null | undefined;
userId: string | null | undefined;
isProtected: boolean;
- hasEmailVerification: boolean;
notionData?: {
rootNotionPageId: string | null;
recordMap: ExtendedRecordMap | null;
};
- token: string | undefined;
- verifiedEmail: string | undefined;
brand?: Brand;
+ token?: string;
+ verifiedEmail?: string;
}) {
const { document, emailProtected, password: linkPassword } = link;
const plausible = usePlausible();
+ const router = useRouter();
const didMount = useRef<boolean>(false);
const [submitted, setSubmitted] = useState<boolean>(false);
@@ -69,13 +69,14 @@ export default function DocumentView({
},
body: JSON.stringify({
...data,
- email: data.email || userEmail,
+ email: data.email || verifiedEmail || userEmail,
linkId: link.id,
documentId: document.id,
userId: userId || null,
documentVersionId: document.versions[0].id,
hasPages: document.versions[0].hasPages,
token: token || null,
+ verifiedEmail: verifiedEmail || null,
}),
});
@@ -108,21 +109,15 @@ export default function DocumentView({
};
// If token is present, run handle submit which will verify token and get document
- useEffect(() => {
- if (token) {
- handleSubmission();
- }
- }, [token]);
-
// If link is not submitted and does not have email / password protection, show the access form
useEffect(() => {
if (!didMount.current) {
- if (!submitted && !isProtected) {
+ if ((!submitted && !isProtected) || token) {
handleSubmission();
}
didMount.current = true;
}
- }, [submitted, isProtected]);
+ }, [submitted, isProtected, token]);
// Components to render when email is submitted but verification is pending
if (verificationRequested) {
diff --git a/pages/api/views.ts b/pages/api/views.ts
index 93a00e85..64531dae 100644
--- a/pages/api/views.ts
+++ b/pages/api/views.ts
@@ -23,6 +23,7 @@ export default async function handle(
documentVersionId,
hasPages,
token,
+ verifiedEmail,
...data
} = req.body as {
linkId: string;
@@ -31,6 +32,7 @@ export default async function handle(
documentVersionId: string;
hasPages: boolean;
token: string | null;
+ verifiedEmail: string | null;
};
const { email, password } = data as { email: string; password: string };
@@ -45,6 +47,8 @@ export default async function handle(
enableNotification: true,
emailAuthenticated: true,
password: true,
+ domainSlug: true,
+ slug: true,
},
});
@@ -86,12 +90,19 @@ export default async function handle(
await prisma.verificationToken.create({
data: {
token,
- identifier: email,
+ identifier: `${linkId}:${email}`,
expires: expiresAt,
},
});
- const verificationUrl = `${process.env.NEXT_PUBLIC_BASE_URL}/view/${linkId}/?token=${token}`;
+ // set the default verification url
+ let verificationUrl: string = `${process.env.NEXT_PUBLIC_BASE_URL}/view/${linkId}/?token=${token}&email=${encodeURIComponent(email)}`;
+
+ if (link.domainSlug && link.slug) {
+ // if custom domain is enabled, use the custom domain
+ verificationUrl = `https://${link.domainSlug}/${link.slug}/?token=${token}&email=${encodeURIComponent(email)}`;
+ }
+
await sendVerificationEmail(email, verificationUrl);
res.status(200).json({
type: "email-verification",
@@ -103,7 +114,10 @@ export default async function handle(
let isEmailVerified: boolean = false;
if (link.emailAuthenticated && token) {
const verification = await prisma.verificationToken.findUnique({
- where: { token: token },
+ where: {
+ token: token,
+ identifier: `${linkId}:${verifiedEmail}`,
+ },
});
if (!verification) {
@@ -113,7 +127,7 @@ export default async function handle(
// Check the token's expiration date
if (Date.now() > verification.expires.getTime()) {
- res.status(401).json({ message: "Token expired" });
+ res.status(401).json({ message: "Access expired" });
return;
}
diff --git a/pages/view/[linkId]/index.tsx b/pages/view/[linkId]/index.tsx
index 073eb397..d21babaf 100644
--- a/pages/view/[linkId]/index.tsx
+++ b/pages/view/[linkId]/index.tsx
@@ -11,7 +11,6 @@ import { GetStaticPropsContext } from "next";
import { useRouter } from "next/router";
import { Brand } from "@prisma/client";
import CustomMetatag from "@/components/view/custom-metatag";
-import Head from "next/head";
export const getStaticProps = async (context: GetStaticPropsContext) => {
const { linkId } = context.params as { linkId: string };
@@ -168,6 +167,8 @@ export default function ViewPage({
isProtected={true}
notionData={notionData}
brand={brand}
+ token={token}
+ verifiedEmail={verifiedEmail}
/>
</>
);
diff --git a/pages/view/domains/[domain]/[slug]/index.tsx b/pages/view/domains/[domain]/[slug]/index.tsx
index 63654497..740443fe 100644
--- a/pages/view/domains/[domain]/[slug]/index.tsx
+++ b/pages/view/domains/[domain]/[slug]/index.tsx
@@ -102,7 +102,10 @@ export default function ViewPage({
}) {
const { data: session, status } = useSession();
const router = useRouter();
- const { authenticationCode } = router.query as { authenticationCode: string };
+ const { token, email: verifiedEmail } = router.query as {
+ token: string;
+ email: string;
+ };
if (!link || status === "loading") {
return (
@@ -162,6 +165,8 @@ export default function ViewPage({
isProtected={true}
notionData={notionData}
brand={brand}
+ token={token}
+ verifiedEmail={verifiedEmail}
/>
</>
);