クラウドスケープウェブサイト認証
CloudScape Website Authentication ジェネレータは、Amazon Cognito を使用してCloudScapeウェブサイトに認証機能を追加します。
このジェネレータは、Cognitoユーザープールと関連するIDプール、ユーザーログインフローを処理するホスト型UI、およびCloudScapeウェブサイトとの統合を設定するCDKインフラストラクチャを構成します。
使い方
CloudScapeウェブサイトに認証を追加する
CloudScapeウェブサイトに認証を追加する方法は2通りあります:
- インストール Nx Console VSCode Plugin まだインストールしていない場合
- VSCodeでNxコンソールを開く
- クリック
Generate (UI)
"Common Nx Commands"セクションで - 検索
@aws/nx-plugin - ts#cloudscape-website#auth
- 必須パラメータを入力
- クリック
Generate
pnpm nx g @aws/nx-plugin:ts#cloudscape-website#auth
yarn nx g @aws/nx-plugin:ts#cloudscape-website#auth
npx nx g @aws/nx-plugin:ts#cloudscape-website#auth
bunx nx g @aws/nx-plugin:ts#cloudscape-website#auth
変更されるファイルを確認するためにドライランを実行することもできます
pnpm nx g @aws/nx-plugin:ts#cloudscape-website#auth --dry-run
yarn nx g @aws/nx-plugin:ts#cloudscape-website#auth --dry-run
npx nx g @aws/nx-plugin:ts#cloudscape-website#auth --dry-run
bunx nx g @aws/nx-plugin:ts#cloudscape-website#auth --dry-run
オプション
パラメータ | 型 | デフォルト | 説明 |
---|---|---|---|
project 必須 | string | - | The root directory of the Cloudscape application. |
cognitoDomain 必須 | string | - | The cognito domain prefix to use |
allowSignup | boolean | Whether to allow self-signup |
ジェネレータの出力内容
Reactウェブサイトに以下の変更が適用されます:
Directorysrc
Directorycomponents
DirectoryCognitoAuth
- index.tsx メインの認証コンポーネント
- main.tsx CognitoAuthコンポーネントを組み込むよう更新
インフラストラクチャコードは packages/common/constructs
に生成されます:
Directorysrc
Directorycore
- user-identity.ts ユーザープールとIDプールを定義するコンストラクト
インフラストラクチャの使用方法
UserIdentity
コンストラクトをウェブサイトコンストラクトの前に宣言する必要があります:
import { Stack } from 'aws-cdk-lib';import { Construct } from 'constructs';import { MyWebsite, UserIdentity } from ':my-scope/common-constructs';
export class ApplicationStack extends Stack { constructor(scope: Construct, id: string) { super(scope, id);
new UserIdentity(this, 'Identity');
new MyWebsite(this, 'MyWebsite'); }}
UserIdentity
コンストラクトは自動的に必要なランタイム設定を追加し、ウェブサイトが正しいCognitoユーザープールを参照できるようにします。
認証済みユーザーへのアクセス権付与
認証済みユーザーにAPI呼び出しなどの特定のアクションを許可するには、IDプールの認証済みロールにIAMポリシーステートメントを追加します:
import { Stack } from 'aws-cdk-lib';import { Construct } from 'constructs';import { MyWebsite, UserIdentity, MyApi } from ':my-scope/common-constructs';
export class ApplicationStack extends Stack { constructor(scope: Construct, id: string) { super(scope, id);
const identity = new UserIdentity(this, 'Identity'); const api = new MyApi(this, 'MyApi');
api.grantInvokeAccess(identity.identityPool.authenticatedRole);
new MyWebsite(this, 'MyWebsite'); }}