React 网站身份验证
React 网站身份验证生成器使用 Amazon Cognito 为您的 React 网站添加身份验证功能。
该生成器会配置 CDK 或 Terraform 基础设施,用于创建 Cognito 用户池及关联的身份池、处理用户登录流程的托管 UI,以及与 React 网站的集成。
为 React 网站添加身份验证
Section titled “为 React 网站添加身份验证”您可以通过两种方式为 React 网站添加身份验证:
- 安装 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. |
您将在 React 网站中看到以下变更:
文件夹src
文件夹components
文件夹CognitoAuth
- index.tsx 主身份验证组件
- main.tsx 更新以集成 CognitoAuth 组件
由于该生成器会根据您选择的 iacProvider
以基础设施即代码的形式输出,它将在 packages/common
目录下创建一个包含相关 CDK 构造体或 Terraform 模块的项目。
通用的基础设施即代码项目结构如下:
文件夹packages/common/constructs
文件夹src
文件夹app/ 针对特定项目/生成器的基础设施构造体
- …
文件夹core/ 被
app
目录构造体重用的通用构造体- …
- index.ts 导出
app
目录构造体的入口文件
- project.json 项目构建目标与配置
文件夹packages/common/terraform
文件夹src
文件夹app/ 针对特定项目/生成器的 Terraform 模块
- …
文件夹core/ 被
app
目录模块重用的通用模块- …
- project.json 项目构建目标与配置
根据您选择的 iacProvider
,您还将看到以下生成的基础设施代码:
文件夹packages/common/constructs/src
文件夹core
- user-identity.ts 定义用户池和身份池的构造器
文件夹packages/common/terraform/src
文件夹core
文件夹user-identity
- main.tf 身份配置的模块封装
文件夹identity
- identity.tf 核心身份基础设施(包含 Cognito 用户池和身份池)
文件夹add-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),可以向身份池的认证角色添加 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}/*" } ] })}