Skip to content

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

feat: add second cookie after signin

Marc Seitz6181dea62aea
diff --git a/pages/api/auth-plus/set-cookie.ts b/pages/api/auth-plus/set-cookie.ts
new file mode 100644
index 00000000..1c28b444
--- /dev/null
+++ b/pages/api/auth-plus/set-cookie.ts
@@ -0,0 +1,24 @@
+import { NextApiRequest, NextApiResponse } from "next";
+
+import { getToken } from "next-auth/jwt";
+
+const VERCEL_DEPLOYMENT = !!process.env.VERCEL_URL;
+
+export default async function handler(
+ req: NextApiRequest,
+ res: NextApiResponse,
+) {
+ const token = await getToken({ req });
+
+ if (!token) {
+ return res.status(401).end();
+ }
+
+ // Set the cookie for the other domain
+ res.setHeader(
+ "Set-Cookie",
+ `${VERCEL_DEPLOYMENT ? "__Secure-" : ""}next-auth.session-token=${req.cookies[`${VERCEL_DEPLOYMENT ? "__Secure-" : ""}next-auth.session-token`]}; HttpOnly; Path=/; SameSite=Lax; ${VERCEL_DEPLOYMENT ? "Secure; " : ""}Domain=.papermark.io; Max-Age=${30 * 24 * 60 * 60}`,
+ );
+
+ res.status(200).end();
+}
diff --git a/pages/api/auth/[...nextauth].ts b/pages/api/auth/[...nextauth].ts
index 9f71c86b..42bb9917 100644
--- a/pages/api/auth/[...nextauth].ts
+++ b/pages/api/auth/[...nextauth].ts
@@ -89,7 +89,7 @@ export const authOptions: NextAuthOptions = {
sameSite: "lax",
path: "/",
// When working on localhost, the cookie domain must be omitted entirely (https://stackoverflow.com/a/1188145)
- domain: VERCEL_DEPLOYMENT ? ".papermark.io" : undefined,
+ domain: VERCEL_DEPLOYMENT ? ".papermark.com" : undefined,
secure: VERCEL_DEPLOYMENT,
},
},
@@ -158,6 +158,13 @@ export const authOptions: NextAuthOptions = {
}
},
async signIn(message) {
+ if (typeof window !== "undefined") {
+ try {
+ await fetch("/api/auth-plus/set-cookie");
+ } catch (error) {
+ console.error("Failed to set additional cookie", error);
+ }
+ }
await identifyUser(message.user.email ?? message.user.id);
await trackAnalytics({
event: "User Signed In",