Skip to content
Open
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
5 changes: 5 additions & 0 deletions .changeset/few-lizards-shake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@aws-blocks/create-blocks-app': patch
---

Store Amplify Cognito runtime configuration in the shared config registry.
6 changes: 6 additions & 0 deletions packages/create-blocks-app/src/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,12 @@ describe('create-blocks-app auto-detection', () => {
const result = run(['-y', '--skip-install'], tmpDir);
assert.strictEqual(result.exitCode, 0);
assert.match(result.stdout, /Detected Amplify Gen 2 project/);

const blocksIntegration = readFileSync(join(tmpDir, 'amplify', 'blocks.ts'), 'utf-8');
assert.match(blocksIntegration, /import \{ registerConfig \} from '@aws-blocks\/blocks\/cdk';/);
assert.match(blocksIntegration, /registerConfig\(blocks, 'COGNITO_USER_POOL_ID', cfnUserPool\.ref\);/);
assert.match(blocksIntegration, /registerConfig\(blocks, 'COGNITO_CLIENT_ID', cfnUserPoolClient\.ref\);/);
assert.doesNotMatch(blocksIntegration, /blocks\.handler\.addEnvironment\('COGNITO_/);
} finally {
rmSync(tmpDir, { recursive: true, force: true, maxRetries: 3, retryDelay: 100 });
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* Passes Cognito config so Blocks can verify bearer tokens.
*/
import { createBlocksBackend } from '../aws-blocks/index.cdk.js';
import { registerConfig } from '@aws-blocks/blocks/cdk';

export async function initBlocks(backend: any) {
const blocksStack = backend.createStack('blocks');
Expand All @@ -14,9 +15,8 @@ export async function initBlocks(backend: any) {
// Pass Amplify's Cognito config to the Blocks Lambda (if auth is configured)
if (backend.auth?.resources?.cfnResources) {
const { cfnUserPool, cfnUserPoolClient } = backend.auth.resources.cfnResources;
blocks.handler.addEnvironment('COGNITO_USER_POOL_ID', cfnUserPool.ref);
blocks.handler.addEnvironment('COGNITO_CLIENT_ID', cfnUserPoolClient.ref);
blocks.handler.addEnvironment('COGNITO_REGION', blocksStack.region);
registerConfig(blocks, 'COGNITO_USER_POOL_ID', cfnUserPool.ref);
registerConfig(blocks, 'COGNITO_CLIENT_ID', cfnUserPoolClient.ref);
}

// Surface Blocks API URL in amplify_outputs.json
Expand Down
6 changes: 3 additions & 3 deletions test-apps/amplify-gen2/amplify/blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* Passes Cognito config so Blocks can verify bearer tokens.
*/
import { createBlocksBackend } from '../aws-blocks/index.cdk.js';
import { registerConfig } from '@aws-blocks/blocks/cdk';

export async function initBlocks(backend: any) {
const blocksStack = backend.createStack('blocks');
Expand All @@ -14,9 +15,8 @@ export async function initBlocks(backend: any) {
// Pass Amplify's Cognito config to the Blocks Lambda (if auth is configured)
if (backend.auth?.resources?.cfnResources) {
const { cfnUserPool, cfnUserPoolClient } = backend.auth.resources.cfnResources;
blocks.handler.addEnvironment('COGNITO_USER_POOL_ID', cfnUserPool.ref);
blocks.handler.addEnvironment('COGNITO_CLIENT_ID', cfnUserPoolClient.ref);
blocks.handler.addEnvironment('COGNITO_REGION', blocksStack.region);
registerConfig(blocks, 'COGNITO_USER_POOL_ID', cfnUserPool.ref);
registerConfig(blocks, 'COGNITO_CLIENT_ID', cfnUserPoolClient.ref);
}

// Surface Blocks API URL in amplify_outputs.json
Expand Down