Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 37 additions & 15 deletions recipe-portal/app/api/execute/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,21 +155,43 @@ export async function POST(request: Request) {
const currentTokenInfo = getCurrentTokenInfo();
console.log('Current token info:', currentTokenInfo);

// Auto-populate authentication variables from current token if not explicitly provided or empty
if (currentTokenInfo && (!envVariables || !envVariables.CLIENT_ID || envVariables.CLIENT_ID.trim() === '')) {
// Replace empty CLIENT_ID with the populated one
envContent = envContent.replace(/^CLIENT_ID=\s*$/m, `CLIENT_ID=${currentTokenInfo.clientId}`);
console.log('Auto-populated CLIENT_ID from token:', currentTokenInfo.clientId?.substring(0,8) + '...');
}
if (currentTokenInfo && (!envVariables || !envVariables.baseURL || envVariables.baseURL.trim() === '')) {
// Replace empty baseURL with the populated one
envContent = envContent.replace(/^baseURL=\s*$/m, `baseURL=${currentTokenInfo.baseURL}`);
console.log('Auto-populated baseURL from token:', currentTokenInfo.baseURL);
}
if (currentTokenInfo && (!envVariables || !envVariables.authURL || envVariables.authURL.trim() === '')) {
// Replace empty authURL with the populated one
envContent = envContent.replace(/^authURL=\s*$/m, `authURL=${currentTokenInfo.authURL}`);
console.log('Auto-populated authURL from token:', currentTokenInfo.authURL);
// --- Auto-populate authentication variables from current token ---
if (currentTokenInfo) {
const BASE_URL_DEFAULT = 'https://aws-api.sigmacomputing.com/v2';
const AUTH_URL_DEFAULT = 'https://aws-api.sigmacomputing.com/v2/auth/token';

// 1. CLIENT_ID: Check for missing or empty value
const isClientIdMissingOrEmpty = !envVariables || !envVariables.CLIENT_ID || (typeof envVariables.CLIENT_ID === 'string' && envVariables.CLIENT_ID.trim() === '');

if (isClientIdMissingOrEmpty && currentTokenInfo.clientId) {
// FIX: Use appending (last value wins) instead of fragile replace.
envContent += `CLIENT_ID=${currentTokenInfo.clientId}\n`;
console.log('Auto-populated CLIENT_ID from token:', currentTokenInfo.clientId?.substring(0,8) + '...');
}

// 2. baseURL: Check for missing/empty OR if the generic default was provided but the token has a specific URL.
const isBaseUrlMissingOrDefault = !envVariables || // envVariables is null/undefined
envVariables.baseURL === undefined || // baseURL key is missing
(typeof envVariables.baseURL === 'string' && envVariables.baseURL.trim() === '') || // baseURL is empty string
(envVariables.baseURL === BASE_URL_DEFAULT && currentTokenInfo.baseURL !== BASE_URL_DEFAULT); // Default but token has regional

if (isBaseUrlMissingOrDefault && currentTokenInfo.baseURL) {
// FIX: Use appending (last value wins) to ensure the token's regional URL is used.
envContent += `baseURL=${currentTokenInfo.baseURL}\n`;
console.log('Auto-populated baseURL from token:', currentTokenInfo.baseURL);
}

// 3. authURL: Check for missing/empty OR if the generic default was provided but the token has a specific URL.
const isAuthUrlMissingOrDefault = !envVariables || // envVariables is null/undefined
envVariables.authURL === undefined ||
(typeof envVariables.authURL === 'string' && envVariables.authURL.trim() === '') || // authURL is empty string
(envVariables.authURL === AUTH_URL_DEFAULT && currentTokenInfo.authURL !== AUTH_URL_DEFAULT); // Default but token has regional

if (isAuthUrlMissingOrDefault && currentTokenInfo.authURL) {
// FIX: Use appending (last value wins) to ensure the token's regional URL is used.
envContent += `authURL=${currentTokenInfo.authURL}\n`;
console.log('Auto-populated authURL from token:', currentTokenInfo.authURL);
}
}

fs.writeFileSync(tempEnvPath, envContent);
Expand Down
35 changes: 18 additions & 17 deletions recipe-portal/components/CodeViewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1175,31 +1175,32 @@ export function CodeViewer({ isOpen, onClose, filePath, fileName, envVariables =
Server Endpoint <span className="text-red-600">*</span>
</label>
<select
value={envValues['baseURL'] || 'https://aws-api.sigmacomputing.com/v2'}
id="server-endpoint-select" // Added id for A11y
value={envValues['baseURL'] || 'https://aws-api.sigmacomputing.com'}
onChange={(e) => {
const baseURL = e.target.value;
let authURL;
if (baseURL === 'https://api.sigmacomputing.com') {
authURL = baseURL + '/v2/auth/token';
} else {
authURL = baseURL + '/auth/token';
}

// ✅ SIMPLIFIED LOGIC: All endpoints now use /v2/auth/token
const AUTH_TOKEN_PATH = '/auth/token';
const authURL = baseURL + AUTH_TOKEN_PATH;

handleEnvChange('baseURL', baseURL);
handleEnvChange('authURL', authURL);
}}
className="w-full px-3 py-2 border border-gray-300 rounded-md text-sm focus:border-blue-500 focus:ring-1 focus:ring-blue-500"
>
<option value="https://api.sigmacomputing.com">GCP hosted organizations</option>

<option value="https://api.sigmacomputing.com/v2">GCP hosted organizations</option>
<option value="https://aws-api.sigmacomputing.com/v2">AWS US (West) hosted organizations</option>
<option value="https://api.us-a.aws.sigmacomputing.com">AWS US (East) hosted organizations</option>
<option value="https://api.ca.aws.sigmacomputing.com">AWS Canada hosted organizations</option>
<option value="https://api.eu.aws.sigmacomputing.com">AWS Europe hosted organizations</option>
<option value="https://api.au.aws.sigmacomputing.com">AWS Australia hosted organizations</option>
<option value="https://api.uk.aws.sigmacomputing.com">AWS UK hosted organizations</option>
<option value="https://api.us.azure.sigmacomputing.com">Azure US hosted organizations</option>
<option value="https://api.eu.azure.sigmacomputing.com">Azure Europe hosted organizations</option>
<option value="https://api.ca.azure.sigmacomputing.com">Azure Canada hosted organizations</option>
<option value="https://api.uk.azure.sigmacomputing.com">Azure UK hosted organizations</option>
<option value="https://api.us-a.aws.sigmacomputing.com/v2">AWS US (East) hosted organizations</option>
<option value="https://api.ca.aws.sigmacomputing.com/v2">AWS Canada hosted organizations</option>
<option value="https://api.eu.aws.sigmacomputing.com/v2">AWS Europe hosted organizations</option>
<option value="https://api.au.aws.sigmacomputing.com/v2">AWS Australia hosted organizations</option>
<option value="https://api.uk.aws.sigmacomputing.com/v2">AWS UK hosted organizations</option>
<option value="https://api.us.azure.sigmacomputing.com/v2">Azure US hosted organizations</option>
<option value="https://api.eu.azure.sigmacomputing.com/v2">Azure Europe hosted organizations</option>
<option value="https://api.ca.azure.sigmacomputing.com/v2">Azure Canada hosted organizations</option>
<option value="https://api.uk.azure.sigmacomputing.com/v2">Azure UK hosted organizations</option>
</select>
</div>

Expand Down
Loading