Skip to content

クラウドスケープ ウェブサイト認証

CloudScape Website Authentication ジェネレータは、Amazon Cognito を使用して CloudScape ウェブサイトに認証機能を追加します。

このジェネレータは、Cognito ユーザープールと関連する Identity Pool、ユーザーログインフローを処理するホステッド UI、および CloudScape ウェブサイトとの統合を作成するための CDK インフラストラクチャを設定します。

CloudScape ウェブサイトに認証を追加する

Section titled “CloudScape ウェブサイトに認証を追加する”

以下の2つの方法で CloudScape ウェブサイトに認証を追加できます:

  1. インストール Nx Console VSCode Plugin まだインストールしていない場合
  2. VSCodeでNxコンソールを開く
  3. クリック Generate (UI) "Common Nx Commands"セクションで
  4. 検索 @aws/nx-plugin - ts#cloudscape-website#auth
  5. 必須パラメータを入力
    • クリック Generate
    パラメータ デフォルト 説明
    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 ユーザープールと Identity Pool を定義するコンストラクト

    インフラストラクチャの使用方法

    Section titled “インフラストラクチャの使用方法”

    UserIdentity コンストラクトをスタックに追加する必要があります。ウェブサイトコンストラクトの前に宣言してください:

    packages/infra/src/stacks/application-stack.ts
    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 ユーザープールを指すようにするための ランタイム設定 を自動的に追加します。

    認証済みユーザーへのアクセス権付与

    Section titled “認証済みユーザーへのアクセス権付与”

    API の呼び出し権限など、認証済みユーザーに特定のアクションを許可するには、Identity Pool の認証済みロールに IAM ポリシーステートメントを追加できます:

    packages/infra/src/stacks/application-stack.ts
    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');
    }
    }