@@ -8,15 +8,23 @@ export async function GET(request) {
88 const state = searchParams . get ( "state" ) // This will be 'gdrive', 'gcalendar', etc.
99 const error = searchParams . get ( "error" )
1010
11- const baseUrl = process . env . APP_BASE_URL
12- if ( ! baseUrl ) {
11+ const publicBaseUrl = process . env . APP_BASE_URL
12+ if ( ! publicBaseUrl ) {
1313 console . error ( "APP_BASE_URL environment variable is not set." )
1414 return new Response ( "Server configuration error." , { status : 500 } )
1515 }
1616
17+ // Determine the correct URL for server-side fetching.
18+ // In a docker-compose self-host setup, containers communicate via internal service names.
19+ // In all other environments (local dev, cloud), use the public URL.
20+ const isSelfHostDocker = process . env . NEXT_PUBLIC_ENVIRONMENT === "selfhost"
21+ const apiUrlForFetch = isSelfHostDocker
22+ ? process . env . INTERNAL_CLIENT_URL
23+ : publicBaseUrl
24+
1725 // FIX: Use the public-facing APP_BASE_URL for redirection, not the internal request.url.
1826 // This ensures the browser is redirected to the correct, publicly accessible address.
19- const integrationsUrl = new URL ( "/integrations" , baseUrl )
27+ const integrationsUrl = new URL ( "/integrations" , publicBaseUrl )
2028
2129 if ( error ) {
2230 // User denied access or an error occurred
@@ -41,7 +49,7 @@ export async function GET(request) {
4149 // The browser session (cookie) is automatically forwarded by Next.js server-side fetch,
4250 // which authenticates the user to our own API proxy.
4351 const apiResponse = await fetch (
44- `${ baseUrl } /api/settings/integrations/connect/oauth` ,
52+ `${ apiUrlForFetch } /api/settings/integrations/connect/oauth` ,
4553 {
4654 method : "POST" ,
4755 headers : {
@@ -51,7 +59,7 @@ export async function GET(request) {
5159 body : JSON . stringify ( {
5260 service_name : state ,
5361 code : code ,
54- redirect_uri : `${ baseUrl } /api/settings/integrations/connect/oauth/callback`
62+ redirect_uri : `${ publicBaseUrl } /api/settings/integrations/connect/oauth/callback`
5563 } )
5664 }
5765 )
0 commit comments