🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
Improve cookies for cloud: multiple subdomains (#366)
Marc Klingen7bcca2a737aa
diff --git a/.env.cloud.example b/.env.cloud.exampleindex 26ca0d56e9..c2589129fd 100644--- a/.env.cloud.example+++ b/.env.cloud.example@@ -1,5 +1,6 @@# Assuming deployment on Vercel with Postgres database on SupabaseNEXT_PUBLIC_LANGFUSE_CLOUD_REGION="US"+NEXTAUTH_COOKIE_DOMAIN=".langfuse.com"# Prismadiff --git a/src/env.mjs b/src/env.mjsindex 203797fd30..2798249d87 100644--- a/src/env.mjs+++ b/src/env.mjs@@ -24,6 +24,7 @@ export const env = createEnv({// VERCEL_URL doesn't include `https` so it can't be validated as a URLprocess.env.VERCEL ? z.string().min(1) : z.string().url(),),+ NEXTAUTH_COOKIE_DOMAIN: z.string().optional(),LANGFUSE_TEAM_SLACK_WEBHOOK: z.string().url().optional(),LANGFUSE_NEW_USER_SIGNUP_WEBHOOK: z.string().url().optional(),// Add `.min(1) on ID and SECRET if you want to make sure they're not empty@@ -52,6 +53,7 @@ export const env = createEnv({DATABASE_URL: process.env.DATABASE_URL,NODE_ENV: process.env.NODE_ENV,NEXTAUTH_SECRET: process.env.NEXTAUTH_SECRET,+ NEXTAUTH_COOKIE_DOMAIN: process.env.NEXTAUTH_COOKIE_DOMAIN,NEXTAUTH_URL: process.env.NEXTAUTH_URL,NEXT_PUBLIC_LANGFUSE_CLOUD_REGION:process.env.NEXT_PUBLIC_LANGFUSE_CLOUD_REGION,diff --git a/src/server/auth.ts b/src/server/auth.tsindex 17139ef43c..39197c1259 100644--- a/src/server/auth.ts+++ b/src/server/auth.ts@@ -10,6 +10,28 @@ import { prisma } from "@/src/server/db";import CredentialsProvider from "next-auth/providers/credentials";import { verifyPassword } from "@/src/features/auth/lib/emailPassword";import { parseFlags } from "@/src/features/feature-flags/utils";+import { env } from "@/src/env.mjs";++// Use secure cookies on https hostnames, exception for Vercel which sets NEXTAUTH_URL without the protocol+const useSecureCookies =+ env.NEXTAUTH_URL.startsWith("https://") || process.env.VERCEL === "1";++const cookieOptions = {+ domain: env.NEXTAUTH_COOKIE_DOMAIN ?? undefined,+ httpOnly: true,+ sameSite: "lax",+ path: "/",+ secure: useSecureCookies,+};++const cookieName = (name: string) =>+ [+ useSecureCookies ? "__Secure-" : "",+ name,+ env.NEXT_PUBLIC_LANGFUSE_CLOUD_REGION+ ? `.${env.NEXT_PUBLIC_LANGFUSE_CLOUD_REGION}`+ : "",+ ].join("");/*** Options for NextAuth.js used to configure adapters, providers, callbacks, etc.@@ -125,6 +147,32 @@ export const authOptions: NextAuthOptions = {pages: {signIn: "/auth/sign-in",},+ cookies: {+ sessionToken: {+ name: cookieName("next-auth.session-token"),+ options: cookieOptions,+ },+ csrfToken: {+ name: cookieName("next-auth.csrf-token"),+ options: cookieOptions,+ },+ callbackUrl: {+ name: cookieName("next-auth.callback-url"),+ options: cookieOptions,+ },+ state: {+ name: cookieName("next-auth.state"),+ options: cookieOptions,+ },+ nonce: {+ name: cookieName("next-auth.nonce"),+ options: cookieOptions,+ },+ pkceCodeVerifier: {+ name: cookieName("next-auth.pkce.code_verifier"),+ options: cookieOptions,+ },+ },};/**