Name:
interface
Value:
Amplify has re-imagined the way frontend developers build fullstack applications. Develop and deploy without the hassle.
Gen1 DocsLegacy

Page updated May 1, 2025

ユーザー属性の検証

defineAuthdefineFunction を使用して、Cognito プリサインアップ Lambda トリガーを作成し、サインアップの動作を拡張して属性値を検証できます。

まず、新しいディレクトリとリソースファイル amplify/auth/pre-sign-up/resource.ts を作成します。次に、defineFunction でこの関数を定義します:

amplify/auth/pre-sign-up/resource.ts
import { defineFunction } from '@aws-amplify/backend';
export const preSignUp = defineFunction({
name: "pre-sign-up",
resourceGroupName: 'auth'
});

次に、対応するハンドラーファイル amplify/auth/pre-sign-up/handler.ts を次の内容で作成します:

amplify/auth/pre-sign-up/handler.ts
import type { PreSignUpTriggerHandler } from "aws-lambda"
function isOlderThan(date: Date, age: number) {
const comparison = new Date()
comparison.setFullYear(comparison.getFullYear() - age)
return date.getTime() <= comparison.getTime()
}
export const handler: PreSignUpTriggerHandler = async (event) => {
const birthdate = new Date(event.request.userAttributes["birthdate"])
// you must be 13 years or older
if (!isOlderThan(birthdate, 13)) {
throw new Error("You must be 13 years or older to use this site")
}
return event
}

最後に、新しく作成した関数リソースを認証リソースに設定します:

amplify/auth/resource.ts
import { defineAuth } from '@aws-amplify/backend';
import { preSignUp } from './pre-sign-up/resource';
export const auth = defineAuth({
// ...
triggers: {
preSignUp
}
});

変更をデプロイすると、ユーザーがサインアップを試行するたびに、このハンドラーは送信者の年齢が 13 歳以上であることを確認します。