Looking for how to use this in your app?See Frontend Libraries →
ユーザーをグループに追加
defineAuth と defineFunction を使用して、Cognito post confirmation Lambda トリガーを作成し、ユーザーが確認されたときに何らかのアクションを実行する動作を拡張できます。
開始するには、AWS SDK v3 パッケージをインストールします。これは auth リソースに対してアクションを実行するために使用され、@types/aws-lambda パッケージはハンドラータイプを定義するために使用されます:
Terminal
npm add --save-dev @aws-sdk/client-cognito-identity-provider @types/aws-lambda次に、新しいディレクトリとリソースファイル amplify/auth/post-confirmation/resource.ts を作成します。次に、defineFunction で Function を定義します:
amplify/auth/post-confirmation/resource.ts
import { defineFunction } from '@aws-amplify/backend';
export const postConfirmation = defineFunction({ name: 'post-confirmation', // optionally define an environment variable for your group name environment: { GROUP_NAME: 'EVERYONE' }, resourceGroupName: 'auth'});Function 定義を作成した後、以下を実行する必要があります:
EVERYONEグループを作成- auth リソースへのアクセスを許可して、
addUserToGroupアクションを実行できるようにする - Function を post confirmation トリガーとして設定
amplify/auth/resource.ts
import { defineAuth } from "@aws-amplify/backend";import { postConfirmation } from "./post-confirmation/resource"
export const auth = defineAuth({ loginWith: { email: true, }, groups: ["EVERYONE"], triggers: { postConfirmation, }, access: (allow) => [ allow.resource(postConfirmation).to(["addUserToGroup"]), ],})次に、Function に対応するハンドラーファイル amplify/auth/post-confirmation/handler.ts を以下の内容で作成します:
amplify/auth/post-confirmation/handler.ts
import type { PostConfirmationTriggerHandler } from 'aws-lambda';import { CognitoIdentityProviderClient, AdminAddUserToGroupCommand} from '@aws-sdk/client-cognito-identity-provider';import { env } from '$amplify/env/post-confirmation';
const client = new CognitoIdentityProviderClient();
// add user to groupexport const handler: PostConfirmationTriggerHandler = async (event) => { const command = new AdminAddUserToGroupCommand({ GroupName: env.GROUP_NAME, Username: event.userName, UserPoolId: event.userPoolId }); const response = await client.send(command); console.log('processed', response.$metadata.requestId); return event;};変更をデプロイした後、ユーザーがサインアップしてアカウントを確認するたびに、「EVERYONE」という名前のグループに自動的に追加されます。