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/apps/editor/lib/auth.ts b/apps/editor/lib/auth.ts index 3160d53b..c71a7363 100644 --- a/apps/editor/lib/auth.ts +++ b/apps/editor/lib/auth.ts @@ -1,13 +1,16 @@ 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 export const auth = createAuth({ db, appName: 'Pascal Editor', - baseURL: process.env.BETTER_AUTH_URL!, + baseURL: BASE_URL, secret: process.env.BETTER_AUTH_SECRET!, sendMagicLink: async ({ email, url }) => { if (!resend) { 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' +})() 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' } /** 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