diff --git a/lib/auth-helpers.ts b/lib/auth-helpers.ts index 51c36f8..ca436b0 100644 --- a/lib/auth-helpers.ts +++ b/lib/auth-helpers.ts @@ -4,18 +4,37 @@ export async function verifyAuth(token: string) { try { let authEndpoint; + const torMode = process.env.NEXT_PUBLIC_TOR_MODE === 'true'; // Check if we're running in a browser environment if (typeof window !== 'undefined') { - // Use the current origin in browser environments - const origin = window.location.origin; - authEndpoint = new URL('/api/auth/me', origin).toString(); - console.log(`Using browser origin for auth endpoint: ${authEndpoint}`); + // For Tor mode in browser, prefer relative URLs + if (torMode) { + authEndpoint = '/api/auth/me'; + console.log(`Tor mode: Using relative URL for auth endpoint: ${authEndpoint}`); + } else { + // Use the current origin in regular browser environments + const origin = window.location.origin; + authEndpoint = new URL('/api/auth/me', origin).toString(); + console.log(`Using browser origin for auth endpoint: ${authEndpoint}`); + } } else { // For SSR or when window is not available (e.g. in Docker container) + + // For Tor mode, always use relative URLs + if (torMode) { + authEndpoint = '/api/auth/me'; + console.log(`Tor mode SSR: Using relative URL for auth endpoint: ${authEndpoint}`); + } // Use the environment variable if available - if (process.env.SERVER_API_URL) { - authEndpoint = `${process.env.SERVER_API_URL}/auth/me`; + else if (process.env.SERVER_API_URL) { + // If SERVER_API_URL already includes /auth/me, don't append it again + if (process.env.SERVER_API_URL.includes('/auth/me')) { + authEndpoint = process.env.SERVER_API_URL; + } else { + // Otherwise append the endpoint to the base URL + authEndpoint = `${process.env.SERVER_API_URL}/auth/me`; + } console.log(`Using SERVER_API_URL for auth endpoint: ${authEndpoint}`); } else { // Fallback for local development