From 7682018c4beae5d456f9e957a196dff52ee6f9ec Mon Sep 17 00:00:00 2001 From: wass08 Date: Wed, 11 Feb 2026 17:40:23 +0900 Subject: [PATCH 1/4] fix magic link issue --- apps/editor/lib/auth.ts | 17 ++++++++++++++--- apps/editor/lib/utils.ts | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 3 deletions(-) diff --git a/apps/editor/lib/auth.ts b/apps/editor/lib/auth.ts index 3160d53b..5cf5a59b 100644 --- a/apps/editor/lib/auth.ts +++ b/apps/editor/lib/auth.ts @@ -1,14 +1,25 @@ import { createAuth } from '@pascal-app/auth/server' import { db } from '@pascal-app/db' import { Resend } from 'resend' +import { BASE_URL } from './utils' -const resend = process.env.RESEND_API_KEY ? new Resend(process.env.RESEND_API_KEY) : null +// Initialize Resend only if API key is available +const resendApiKey = process.env.RESEND_API_KEY +const resend = resendApiKey && resendApiKey.trim() !== '' ? new Resend(resendApiKey) : null + +// Better Auth secret is required +const betterAuthSecret = process.env.BETTER_AUTH_SECRET +if (!betterAuthSecret) { + throw new Error( + 'Missing BETTER_AUTH_SECRET environment variable. Generate one with: openssl rand -base64 32', + ) +} export const auth = createAuth({ db, appName: 'Pascal Editor', - baseURL: process.env.BETTER_AUTH_URL!, - secret: process.env.BETTER_AUTH_SECRET!, + baseURL: BASE_URL, + secret: betterAuthSecret, sendMagicLink: async ({ email, url }) => { if (!resend) { console.log(`[DEV] Magic link for ${email}: ${url}`) diff --git a/apps/editor/lib/utils.ts b/apps/editor/lib/utils.ts index d32b0fe6..44aa86a2 100644 --- a/apps/editor/lib/utils.ts +++ b/apps/editor/lib/utils.ts @@ -4,3 +4,41 @@ import { twMerge } from 'tailwind-merge' export function cn(...inputs: ClassValue[]) { return twMerge(clsx(inputs)) } + +export const isDevelopment = + process.env.NODE_ENV === 'development' || + process.env.NEXT_PUBLIC_VERCEL_ENV === 'development' + +export const isProduction = + process.env.NODE_ENV === 'production' || process.env.NEXT_PUBLIC_VERCEL_ENV === 'production' + +export const isPreview = process.env.NEXT_PUBLIC_VERCEL_ENV === 'preview' + +/** + * Base URL for the application + * Uses NEXT_PUBLIC_* variables which are available at build time + */ +export const BASE_URL = (() => { + // Development: localhost + if (isDevelopment) { + return process.env.NEXT_PUBLIC_APP_URL || `http://localhost:${process.env.PORT || 3000}` + } + + // Preview deployments: use Vercel branch URL + if (isPreview && process.env.NEXT_PUBLIC_VERCEL_URL) { + return `https://${process.env.NEXT_PUBLIC_VERCEL_URL}` + } + + // Production: use custom domain or Vercel production URL + if (isProduction) { + return ( + process.env.NEXT_PUBLIC_APP_URL || + (process.env.NEXT_PUBLIC_VERCEL_PROJECT_PRODUCTION_URL + ? `https://${process.env.NEXT_PUBLIC_VERCEL_PROJECT_PRODUCTION_URL}` + : 'https://editor.pascal.app') + ) + } + + // Fallback (should never reach here in normal operation) + return process.env.NEXT_PUBLIC_APP_URL || 'http://localhost:3000' +})() From addcdd76e0d9d90702414e56a95ccbaa37ca8603 Mon Sep 17 00:00:00 2001 From: wass08 Date: Wed, 11 Feb 2026 17:44:51 +0900 Subject: [PATCH 2/4] another hope? --- apps/editor/lib/auth.ts | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/apps/editor/lib/auth.ts b/apps/editor/lib/auth.ts index 5cf5a59b..c71a7363 100644 --- a/apps/editor/lib/auth.ts +++ b/apps/editor/lib/auth.ts @@ -7,19 +7,11 @@ import { BASE_URL } from './utils' const resendApiKey = process.env.RESEND_API_KEY const resend = resendApiKey && resendApiKey.trim() !== '' ? new Resend(resendApiKey) : null -// Better Auth secret is required -const betterAuthSecret = process.env.BETTER_AUTH_SECRET -if (!betterAuthSecret) { - throw new Error( - 'Missing BETTER_AUTH_SECRET environment variable. Generate one with: openssl rand -base64 32', - ) -} - export const auth = createAuth({ db, appName: 'Pascal Editor', baseURL: BASE_URL, - secret: betterAuthSecret, + secret: process.env.BETTER_AUTH_SECRET!, sendMagicLink: async ({ email, url }) => { if (!resend) { console.log(`[DEV] Magic link for ${email}: ${url}`) From 9bc65f38c1fa103384c47e64944a8e772286636d Mon Sep 17 00:00:00 2001 From: wass08 Date: Wed, 11 Feb 2026 18:06:17 +0900 Subject: [PATCH 3/4] fix base_url for preview branches --- packages/db/supabase/.temp/gotrue-version | 1 + packages/db/supabase/.temp/pooler-url | 1 + packages/db/supabase/.temp/postgres-version | 1 + packages/db/supabase/.temp/project-ref | 1 + packages/db/supabase/.temp/rest-version | 1 + packages/db/supabase/.temp/storage-migration | 1 + packages/db/supabase/.temp/storage-version | 1 + 7 files changed, 7 insertions(+) create mode 100644 packages/db/supabase/.temp/gotrue-version create mode 100644 packages/db/supabase/.temp/pooler-url create mode 100644 packages/db/supabase/.temp/postgres-version create mode 100644 packages/db/supabase/.temp/project-ref create mode 100644 packages/db/supabase/.temp/rest-version create mode 100644 packages/db/supabase/.temp/storage-migration create mode 100644 packages/db/supabase/.temp/storage-version diff --git a/packages/db/supabase/.temp/gotrue-version b/packages/db/supabase/.temp/gotrue-version new file mode 100644 index 00000000..42149282 --- /dev/null +++ b/packages/db/supabase/.temp/gotrue-version @@ -0,0 +1 @@ +v2.186.0 \ No newline at end of file diff --git a/packages/db/supabase/.temp/pooler-url b/packages/db/supabase/.temp/pooler-url new file mode 100644 index 00000000..0220427b --- /dev/null +++ b/packages/db/supabase/.temp/pooler-url @@ -0,0 +1 @@ +postgresql://postgres.byrpxoiotywskoojsrzd@aws-1-us-east-1.pooler.supabase.com:5432/postgres \ No newline at end of file diff --git a/packages/db/supabase/.temp/postgres-version b/packages/db/supabase/.temp/postgres-version new file mode 100644 index 00000000..3dc41f46 --- /dev/null +++ b/packages/db/supabase/.temp/postgres-version @@ -0,0 +1 @@ +17.6.1.063 \ No newline at end of file diff --git a/packages/db/supabase/.temp/project-ref b/packages/db/supabase/.temp/project-ref new file mode 100644 index 00000000..4315491e --- /dev/null +++ b/packages/db/supabase/.temp/project-ref @@ -0,0 +1 @@ +byrpxoiotywskoojsrzd \ No newline at end of file diff --git a/packages/db/supabase/.temp/rest-version b/packages/db/supabase/.temp/rest-version new file mode 100644 index 00000000..35204367 --- /dev/null +++ b/packages/db/supabase/.temp/rest-version @@ -0,0 +1 @@ +v14.1 \ No newline at end of file diff --git a/packages/db/supabase/.temp/storage-migration b/packages/db/supabase/.temp/storage-migration new file mode 100644 index 00000000..b781586e --- /dev/null +++ b/packages/db/supabase/.temp/storage-migration @@ -0,0 +1 @@ +fix-optimized-search-function \ No newline at end of file diff --git a/packages/db/supabase/.temp/storage-version b/packages/db/supabase/.temp/storage-version new file mode 100644 index 00000000..f663f9d2 --- /dev/null +++ b/packages/db/supabase/.temp/storage-version @@ -0,0 +1 @@ +v1.37.7 \ No newline at end of file From e0c9d40bcf235843fdb495c7c171770b62362032 Mon Sep 17 00:00:00 2001 From: wass08 Date: Wed, 11 Feb 2026 18:21:34 +0900 Subject: [PATCH 4/4] Fix localhost hardcoding in auth - use Vercel env vars --- .../features/community/lib/auth/server.ts | 5 ++-- packages/auth/src/client.ts | 28 +++++++++++++++++-- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/apps/editor/features/community/lib/auth/server.ts b/apps/editor/features/community/lib/auth/server.ts index 301b4835..aa7b0999 100644 --- a/apps/editor/features/community/lib/auth/server.ts +++ b/apps/editor/features/community/lib/auth/server.ts @@ -1,6 +1,5 @@ import { headers as nextHeaders } from 'next/headers' - -const API_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:3000' +import { BASE_URL } from '@/lib/utils' /** * Get the current session from Better Auth backend (server-side) @@ -10,7 +9,7 @@ export async function getSession() { const headersList = await nextHeaders() // Make authenticated request to the auth backend to get session - const response = await fetch(`${API_URL}/api/auth/get-session`, { + const response = await fetch(`${BASE_URL}/api/auth/get-session`, { headers: { cookie: headersList.get('cookie') || '', }, diff --git a/packages/auth/src/client.ts b/packages/auth/src/client.ts index 7994f42d..633f1559 100644 --- a/packages/auth/src/client.ts +++ b/packages/auth/src/client.ts @@ -11,8 +11,32 @@ function getAuthURL(): string { return window.location.origin } - // SSR fallback - return process.env.BETTER_AUTH_URL || 'http://localhost:3000' + // SSR fallback - detect environment from Vercel variables + const isDevelopment = + process.env.NODE_ENV === 'development' || + process.env.NEXT_PUBLIC_VERCEL_ENV === 'development' + const isPreview = process.env.NEXT_PUBLIC_VERCEL_ENV === 'preview' + const isProduction = + process.env.NODE_ENV === 'production' || process.env.NEXT_PUBLIC_VERCEL_ENV === 'production' + + if (isDevelopment) { + return process.env.NEXT_PUBLIC_APP_URL || `http://localhost:${process.env.PORT || 3000}` + } + + if (isPreview && process.env.NEXT_PUBLIC_VERCEL_URL) { + return `https://${process.env.NEXT_PUBLIC_VERCEL_URL}` + } + + if (isProduction) { + return ( + process.env.NEXT_PUBLIC_APP_URL || + (process.env.NEXT_PUBLIC_VERCEL_PROJECT_PRODUCTION_URL + ? `https://${process.env.NEXT_PUBLIC_VERCEL_PROJECT_PRODUCTION_URL}` + : 'https://editor.pascal.app') + ) + } + + return 'http://localhost:3000' } /**