Looking for how to use this in your app?See Frontend Libraries →
メールドメインフィルタリング
defineAuth と defineFunction を使用して、ユーザーのメールアドレスに基づいてフィルタリングを実行する Cognito pre sign-up Lambda トリガー を作成できます。これにより、メールアドレスに基づいてユーザーサインアップを許可または拒否できます。
開始するには、ハンドラー型を定義するために使用される aws-lambda パッケージをインストールします。
Terminal
npm add --save-dev @types/aws-lambda次に、新しいディレクトリとリソースファイル amplify/auth/pre-sign-up/resource.ts を作成します。次に、defineFunction で Function を定義します。
amplify/auth/pre-sign-up/resource.ts
import { defineFunction } from '@aws-amplify/backend';
export const preSignUp = defineFunction({ name: 'pre-sign-up', // optionally define an environment variable for your filter environment: { ALLOW_DOMAIN: 'amazon.com' }});次に、対応するハンドラーファイル amplify/auth/pre-sign-up/handler.ts を以下の内容で作成します。
amplify/auth/pre-sign-up/handler.ts
import type { PreSignUpTriggerHandler } from 'aws-lambda';import { env } from '$amplify/env/pre-sign-up';
export const handler: PreSignUpTriggerHandler = async (event) => { const email = event.request.userAttributes['email'];
if (!email.endsWith(env.ALLOW_DOMAIN)) { throw new Error('Invalid email domain'); }
return event;};最後に、新しく作成した Function リソースを auth リソースに設定します。
amplify/auth/resource.ts
import { defineAuth } from '@aws-amplify/backend';import { preSignUp } from './pre-sign-up/resource';
export const auth = defineAuth({ // ... triggers: { preSignUp }});変更をデプロイした後、amazon.com メールアドレスを持たずにサインアップしようとするユーザーはエラーを受け取ります。