From 4451eddc91fadba23e6f68a80119427e2f31b446 Mon Sep 17 00:00:00 2001 From: Ajax Davis Date: Fri, 2 Jan 2026 00:12:55 +1000 Subject: [PATCH] fix: use custom domain for auth baseURL in production VERCEL_URL returns deployment URL (tpmjs-xxx.vercel.app) not custom domain. This was causing session cookies to be set for wrong domain. Now prioritizes BETTER_AUTH_URL, then checks VERCEL_ENV=production to use tpmjs.com. --- apps/web/next-env.d.ts | 2 +- apps/web/src/lib/auth.ts | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/apps/web/next-env.d.ts b/apps/web/next-env.d.ts index 9edff1c..c4b7818 100644 --- a/apps/web/next-env.d.ts +++ b/apps/web/next-env.d.ts @@ -1,6 +1,6 @@ /// /// -import "./.next/types/routes.d.ts"; +import "./.next/dev/types/routes.d.ts"; // NOTE: This file should not be edited // see https://nextjs.org/docs/app/api-reference/config/typescript for more information. diff --git a/apps/web/src/lib/auth.ts b/apps/web/src/lib/auth.ts index 553af54..5917597 100644 --- a/apps/web/src/lib/auth.ts +++ b/apps/web/src/lib/auth.ts @@ -3,9 +3,14 @@ import { betterAuth } from 'better-auth'; import { prismaAdapter } from 'better-auth/adapters/prisma'; import { sendVerificationEmail } from './email'; -// Determine base URL: use env var, or Vercel URL, or default to production +// Determine base URL for auth - MUST match the domain users are browsing on +// VERCEL_URL is the deployment URL (e.g., tpmjs-xxx.vercel.app), not the custom domain +// So we prioritize BETTER_AUTH_URL or fall back to the production domain const getBaseURL = () => { if (process.env.BETTER_AUTH_URL) return process.env.BETTER_AUTH_URL; + // In production, always use the custom domain, not VERCEL_URL + if (process.env.VERCEL_ENV === 'production') return 'https://tpmjs.com'; + // For preview deployments, use the Vercel URL if (process.env.VERCEL_URL) return `https://${process.env.VERCEL_URL}`; return 'https://tpmjs.com'; };