Bỏ qua để đến nội dung

Xác thực Website React

Generator Xác thực Website React thêm tính năng xác thực vào website React của bạn bằng cách sử dụng Amazon Cognito.

Generator này cấu hình hạ tầng CDK hoặc Terraform để tạo Cognito User Pool và Identity Pool liên quan, cũng như giao diện người dùng được lưu trữ để xử lý luồng đăng nhập của người dùng và tích hợp với website React của bạn.

Bạn có thể thêm xác thực vào website React của mình theo hai cách:

  1. Install the Nx Console VSCode Plugin if you haven't already
  2. Open the Nx Console in VSCode
  3. Click Generate (UI) in the "Common Nx Commands" section
  4. Search for @aws/nx-plugin - ts#react-website#auth
  5. Fill in the required parameters
    • Click Generate
    Parameter Type Default Description
    project Required string - The root directory of the React website.
    cognitoDomain Required 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.

    Bạn sẽ thấy các thay đổi sau trong website React của mình:

    • Thư mụcsrc
      • Thư mụccomponents
        • Thư mụcCognitoAuth
          • index.tsx Component xác thực chính
      • main.tsx Được cập nhật để tích hợp component CognitoAuth

    Vì generator này cung cấp infrastructure as code dựa trên iacProvider bạn đã chọn, nó sẽ tạo một dự án trong packages/common bao gồm các CDK constructs hoặc Terraform modules liên quan.

    Dự án infrastructure as code chung được cấu trúc như sau:

    • Thư mụcpackages/common/constructs
      • Thư mụcsrc
        • Thư mụcapp/ Constructs cho infrastructure cụ thể của một dự án/generator
        • Thư mụccore/ Constructs chung được tái sử dụng bởi các constructs trong app
        • index.ts Entry point xuất các constructs từ app
      • project.json Các build targets và cấu hình của dự án

    Bạn cũng sẽ thấy mã hạ tầng sau được tạo dựa trên iacProvider bạn đã chọn:

    • Thư mụcpackages/common/constructs/src
      • Thư mụccore
        • user-identity.ts Construct định nghĩa user pool và identity pool

    Bạn cần thêm hạ tầng user identity vào stack của mình, khai báo nó trước website:

    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');
    }
    }

    Construct UserIdentity tự động thêm Cấu hình Runtime cần thiết để đảm bảo rằng website của bạn có thể trỏ đến đúng Cognito User Pool cho xác thực.

    Cấp Quyền Truy cập cho Người dùng Đã xác thực

    Phần tiêu đề “Cấp Quyền Truy cập cho Người dùng Đã xác thực”

    Để cấp cho người dùng đã xác thực quyền thực hiện các hành động nhất định, chẳng hạn như cấp quyền gọi API, bạn có thể thêm các câu lệnh IAM policy vào vai trò đã xác thực của identity pool:

    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');
    }
    }