Reactウェブサイト認証
React Website Authentication ジェネレータは Amazon Cognito を使用して React ウェブサイトに認証機能を追加します。
このジェネレータは、Cognito ユーザープールと関連する Identity Pool、ユーザーログインフローを処理するホスト型 UI、および React ウェブサイトとの統合を設定する CDK または Terraform インフラストラクチャを構成します。
React ウェブサイトに認証を追加する
Section titled “React ウェブサイトに認証を追加する”React ウェブサイトに認証を追加する方法は2通りあります:
- インストール Nx Console VSCode Plugin まだインストールしていない場合
- VSCodeでNxコンソールを開く
- クリック
Generate (UI)
"Common Nx Commands"セクションで - 検索
@aws/nx-plugin - ts#react-website#auth
- 必須パラメータを入力
- クリック
Generate
pnpm nx g @aws/nx-plugin:ts#react-website#auth
yarn nx g @aws/nx-plugin:ts#react-website#auth
npx nx g @aws/nx-plugin:ts#react-website#auth
bunx nx g @aws/nx-plugin:ts#react-website#auth
変更されるファイルを確認するためにドライランを実行することもできます
pnpm nx g @aws/nx-plugin:ts#react-website#auth --dry-run
yarn nx g @aws/nx-plugin:ts#react-website#auth --dry-run
npx nx g @aws/nx-plugin:ts#react-website#auth --dry-run
bunx nx g @aws/nx-plugin:ts#react-website#auth --dry-run
パラメータ | 型 | デフォルト | 説明 |
---|---|---|---|
project 必須 | string | - | The root directory of the React website. |
cognitoDomain 必須 | string | - | The cognito domain prefix to use |
allowSignup | boolean | Whether to allow self-signup | |
iacProvider | string | Inherit | The preferred IaC provider. By default this is inherited from your initial selection. |
ジェネレータの出力内容
Section titled “ジェネレータの出力内容”React ウェブサイトに以下の変更が加えられます:
Directorysrc
Directorycomponents
DirectoryCognitoAuth
- index.tsx メインの認証コンポーネント
- main.tsx CognitoAuth コンポーネントを組み込むように更新
インフラストラクチャ
Section titled “インフラストラクチャ”このジェネレータは選択した iacProvider
に基づいてInfrastructure as Codeを生成するため、packages/common
に関連するCDKコンストラクトまたはTerraformモジュールを含むプロジェクトを作成します。
共通のInfrastructure as Codeプロジェクトは以下の構造を持ちます:
Directorypackages/common/constructs
Directorysrc
Directoryapp/ プロジェクト/ジェネレータ固有のインフラストラクチャ用コンストラクト
- …
Directorycore/
app
内のコンストラクトで再利用される汎用コンストラクト- …
- index.ts
app
からコンストラクトをエクスポートするエントリーポイント
- project.json プロジェクトのビルドターゲットと設定
Directorypackages/common/terraform
Directorysrc
Directoryapp/ プロジェクト/ジェネレータ固有のインフラストラクチャ用Terraformモジュール
- …
Directorycore/
app
内のモジュールで再利用される汎用モジュール- …
- project.json プロジェクトのビルドターゲットと設定
選択した iacProvider
に基づいて以下のインフラストラクチャコードが生成されます:
Directorypackages/common/constructs/src
Directorycore
- user-identity.ts ユーザープールと Identity Pool を定義するコンストラクト
Directorypackages/common/terraform/src
Directorycore
Directoryuser-identity
- main.tf アイデンティティ設定のモジュールラッパー
Directoryidentity
- identity.tf Cognito ユーザープールと Identity Pool を含むコアアイデンティティインフラ
Directoryadd-callback-url
- add-callback-url.tf 既存ユーザープールクライアントにコールバックURLを追加するモジュール
インフラストラクチャの使用方法
Section titled “インフラストラクチャの使用方法”ユーザーアイデンティティインフラをウェブサイトの前に宣言してスタックに追加する必要があります:
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 ユーザープールを指すようにするためのランタイム設定を自動的に追加します。
ユーザーアイデンティティモジュールを追加し、ウェブサイトがそれに依存するように設定します:
# ランタイム設定に追加するため最初にユーザーアイデンティティをデプロイmodule "user_identity" { source = "../../common/terraform/src/core/user-identity"}
# ランタイム設定を含めるためアイデンティティの後にウェブサイトをデプロイmodule "my_website" { source = "../../common/terraform/src/app/static-websites/my-website"
providers = { aws.us_east_1 = aws.us_east_1 }
# ランタイム設定に追加するためアイデンティティの先行デプロイを保証 depends_on = [module.user_identity]}
ユーザーアイデンティティモジュールは、ウェブサイトが正しい Cognito ユーザープールを指すようにするためのランタイム設定を自動的に追加します。
認証ユーザーへのアクセス権付与
Section titled “認証ユーザーへのアクセス権付与”APIの呼び出し権限を付与するなど、認証ユーザーに特定のアクションを許可するには、Identity Pool の認証済みロールに 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'); }}
module "user_identity" { source = "../../common/terraform/src/core/user-identity"}
module "my_api" { source = "../../common/terraform/src/app/apis/my-api"}
# 認証ユーザーがFast APIを呼び出す権限を追加resource "aws_iam_role_policy" "authenticated_fast_api_invoke" { name = "authenticated-user-invoke-my-api" role = module.user_identity.authenticated_role_name
policy = jsonencode({ Version = "2012-10-17" Statement = [ { Effect = "Allow" Action = [ "execute-api:Invoke" ] Resource = "${module.my_api.api_execution_arn}/*" } ] })}