From 98853607051b74b571bb062395295b6cc73700a3 Mon Sep 17 00:00:00 2001 From: NotII <46204250+NotII@users.noreply.github.com> Date: Mon, 10 Mar 2025 18:32:28 +0000 Subject: [PATCH] Update auth-helpers.ts --- lib/auth-helpers.ts | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) 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