Skip to content

Commit c800f3e

Browse files
fix: update category initializer to take specific auth modes in api init
1 parent eab9036 commit c800f3e

1 file changed

Lines changed: 20 additions & 16 deletions

File tree

packages/amplify-gen2-migration-e2e-system/src/core/category-initializer.ts

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import {
2020
addAuthWithEmail,
2121
addAuthWithGroups,
2222
addApi,
23-
addApiWithBlankSchema,
2423
addRestApi,
2524
addS3Storage,
2625
addS3StorageWithAuthOnly,
@@ -196,23 +195,28 @@ export class CategoryInitializer {
196195
this.logger.info('Initializing GraphQL API category...', context);
197196

198197
try {
199-
// Determine which auth modes the API needs based on config
200-
const needsCognitoAuth = apiConfig.authModes?.includes('COGNITO_USER_POOLS');
201-
const needsIamAuth = apiConfig.authModes?.includes('IAM');
202-
203-
if (needsCognitoAuth || needsIamAuth) {
204-
// Use addApi with explicit auth types config.
205-
// Pass requireAuthSetup = false because the auth category is already initialized,
206-
// so the CLI won't prompt for Cognito setup — it reuses the existing user pool.
207-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
208-
const authTypesConfig: Record<string, Record<string, unknown>> = { 'API key': {} };
209-
if (needsCognitoAuth) authTypesConfig['Amazon Cognito User Pool'] = {};
210-
if (needsIamAuth) authTypesConfig['IAM'] = {};
211-
await addApi(appPath, authTypesConfig, false);
212-
} else {
213-
await addApiWithBlankSchema(appPath);
198+
// Build authTypesConfig in the order specified by migration-config.json so the
199+
// first auth mode becomes the default (addApi uses the first key as default).
200+
const authModeMap: Record<string, string> = {
201+
IAM: 'IAM',
202+
API_KEY: 'API key',
203+
COGNITO_USER_POOLS: 'Amazon Cognito User Pool',
204+
};
205+
206+
const authTypesConfig: Record<string, Record<string, unknown>> = {};
207+
for (const mode of apiConfig.authModes ?? []) {
208+
const mapped = authModeMap[mode];
209+
if (mapped) authTypesConfig[mapped] = {};
210+
}
211+
212+
// Fallback: ensure at least API key is present
213+
if (Object.keys(authTypesConfig).length === 0) {
214+
authTypesConfig['API key'] = {};
214215
}
215216

217+
// Pass requireAuthSetup = false because the auth category is already initialized
218+
await addApi(appPath, authTypesConfig, false);
219+
216220
// If a schema file is specified, update the schema
217221
if (apiConfig.schema) {
218222
const schemaPath = path.join(appPath, apiConfig.schema);

0 commit comments

Comments
 (0)