React 웹사이트 인증
React 웹사이트 인증 생성기는 Amazon Cognito를 사용하여 React 웹사이트에 인증 기능을 추가합니다.
이 생성기는 CDK 또는 Terraform 인프라를 구성하여 Cognito 사용자 풀 및 관련 Identity Pool, 사용자 로그인 흐름을 처리하는 호스팅된 UI, 그리고 React 웹사이트와의 통합을 설정합니다.
사용 방법
섹션 제목: “사용 방법”React 웹사이트에 인증 추가하기
섹션 제목: “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 사용자 풀 및 Identity Pool을 정의하는 Construct
디렉터리packages/common/terraform/src
디렉터리core
디렉터리user-identity
- main.tf Identity 구성을 위한 모듈 래퍼
디렉터리identity
- identity.tf Cognito 사용자 풀 및 Identity Pool을 포함한 핵심 Identity 인프라
디렉터리add-callback-url
- add-callback-url.tf 기존 사용자 풀 클라이언트에 콜백 URL 추가 모듈
인프라 사용 방법
섹션 제목: “인프라 사용 방법”웹사이트 선언 이전에 사용자 Identity 인프라를 스택에 추가해야 합니다:
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
Construct는 웹사이트가 올바른 Cognito 사용자 풀을 가리킬 수 있도록 필요한 런타임 구성을 자동으로 추가합니다.
사용자 Identity 모듈을 추가하고 웹사이트가 해당 모듈에 종속되도록 설정해야 합니다:
# 런타임 구성 추가를 위해 먼저 사용자 Identity 배포module "user_identity" { source = "../../common/terraform/src/core/user-identity"}
# 런타임 구성을 포함하기 위해 Identity 이후에 웹사이트 배포module "my_website" { source = "../../common/terraform/src/app/static-websites/my-website"
providers = { aws.us_east_1 = aws.us_east_1 }
# 런타임 구성 추가를 위해 Identity 먼저 배포 확인 depends_on = [module.user_identity]}
사용자 Identity 모듈은 웹사이트가 올바른 Cognito 사용자 풀을 가리킬 수 있도록 필요한 런타임 구성을 자동으로 추가합니다.
인증된 사용자에게 액세스 권한 부여
섹션 제목: “인증된 사용자에게 액세스 권한 부여”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}/*" } ] })}