@@ -155,21 +155,43 @@ export async function POST(request: Request) {
155155 const currentTokenInfo = getCurrentTokenInfo ( ) ;
156156 console . log ( 'Current token info:' , currentTokenInfo ) ;
157157
158- // Auto-populate authentication variables from current token if not explicitly provided or empty
159- if ( currentTokenInfo && ( ! envVariables || ! envVariables . CLIENT_ID || envVariables . CLIENT_ID . trim ( ) === '' ) ) {
160- // Replace empty CLIENT_ID with the populated one
161- envContent = envContent . replace ( / ^ C L I E N T _ I D = \s * $ / m, `CLIENT_ID=${ currentTokenInfo . clientId } ` ) ;
162- console . log ( 'Auto-populated CLIENT_ID from token:' , currentTokenInfo . clientId ?. substring ( 0 , 8 ) + '...' ) ;
163- }
164- if ( currentTokenInfo && ( ! envVariables || ! envVariables . baseURL || envVariables . baseURL . trim ( ) === '' ) ) {
165- // Replace empty baseURL with the populated one
166- envContent = envContent . replace ( / ^ b a s e U R L = \s * $ / m, `baseURL=${ currentTokenInfo . baseURL } ` ) ;
167- console . log ( 'Auto-populated baseURL from token:' , currentTokenInfo . baseURL ) ;
168- }
169- if ( currentTokenInfo && ( ! envVariables || ! envVariables . authURL || envVariables . authURL . trim ( ) === '' ) ) {
170- // Replace empty authURL with the populated one
171- envContent = envContent . replace ( / ^ a u t h U R L = \s * $ / m, `authURL=${ currentTokenInfo . authURL } ` ) ;
172- console . log ( 'Auto-populated authURL from token:' , currentTokenInfo . authURL ) ;
158+ // --- Auto-populate authentication variables from current token ---
159+ if ( currentTokenInfo ) {
160+ const BASE_URL_DEFAULT = 'https://aws-api.sigmacomputing.com/v2' ;
161+ const AUTH_URL_DEFAULT = 'https://aws-api.sigmacomputing.com/v2/auth/token' ;
162+
163+ // 1. CLIENT_ID: Check for missing or empty value
164+ const isClientIdMissingOrEmpty = ! envVariables || ! envVariables . CLIENT_ID || ( typeof envVariables . CLIENT_ID === 'string' && envVariables . CLIENT_ID . trim ( ) === '' ) ;
165+
166+ if ( isClientIdMissingOrEmpty && currentTokenInfo . clientId ) {
167+ // FIX: Use appending (last value wins) instead of fragile replace.
168+ envContent += `CLIENT_ID=${ currentTokenInfo . clientId } \n` ;
169+ console . log ( 'Auto-populated CLIENT_ID from token:' , currentTokenInfo . clientId ?. substring ( 0 , 8 ) + '...' ) ;
170+ }
171+
172+ // 2. baseURL: Check for missing/empty OR if the generic default was provided but the token has a specific URL.
173+ const isBaseUrlMissingOrDefault = ! envVariables || // envVariables is null/undefined
174+ envVariables . baseURL === undefined || // baseURL key is missing
175+ ( typeof envVariables . baseURL === 'string' && envVariables . baseURL . trim ( ) === '' ) || // baseURL is empty string
176+ ( envVariables . baseURL === BASE_URL_DEFAULT && currentTokenInfo . baseURL !== BASE_URL_DEFAULT ) ; // Default but token has regional
177+
178+ if ( isBaseUrlMissingOrDefault && currentTokenInfo . baseURL ) {
179+ // FIX: Use appending (last value wins) to ensure the token's regional URL is used.
180+ envContent += `baseURL=${ currentTokenInfo . baseURL } \n` ;
181+ console . log ( 'Auto-populated baseURL from token:' , currentTokenInfo . baseURL ) ;
182+ }
183+
184+ // 3. authURL: Check for missing/empty OR if the generic default was provided but the token has a specific URL.
185+ const isAuthUrlMissingOrDefault = ! envVariables || // envVariables is null/undefined
186+ envVariables . authURL === undefined ||
187+ ( typeof envVariables . authURL === 'string' && envVariables . authURL . trim ( ) === '' ) || // authURL is empty string
188+ ( envVariables . authURL === AUTH_URL_DEFAULT && currentTokenInfo . authURL !== AUTH_URL_DEFAULT ) ; // Default but token has regional
189+
190+ if ( isAuthUrlMissingOrDefault && currentTokenInfo . authURL ) {
191+ // FIX: Use appending (last value wins) to ensure the token's regional URL is used.
192+ envContent += `authURL=${ currentTokenInfo . authURL } \n` ;
193+ console . log ( 'Auto-populated authURL from token:' , currentTokenInfo . authURL ) ;
194+ }
173195 }
174196
175197 fs . writeFileSync ( tempEnvPath , envContent ) ;
0 commit comments