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

ユーザー属性の管理

メールアドレスや電話番号などのユーザー属性は、個々のユーザーを識別するのに役立ちます。ユーザープロファイルに含めるユーザー属性を定義することで、ユーザーデータを大規模に管理しやすくなります。この情報は、ユーザージャーニーをパーソナライズし、コンテンツを調整し、直感的なアカウント管理を提供するなど、さまざまな用途に役立ちます。サインアップ時に事前に情報を収集することも、サインアップ後に顧客がプロファイルを更新できるようにすることもできます。このセクションでは、ユーザー属性の操作方法、設定方法、管理方法について詳しく説明します。

サインアップ時にカスタムユーザー属性を設定する

カスタム属性は、signUp APIのuserAttributesオプションで渡すことができます。

Future<void> _signUp({
required String username,
required String password,
required String email,
required String customValue,
}) async {
final userAttributes = {
AuthUserAttributeKey.email: email,
// Create and pass a custom attribute
const CognitoUserAttributeKey.custom('my-custom-attribute'): customValue
};
await Amplify.Auth.signUp(
username: username,
password: password,
options: SignUpOptions(
userAttributes: userAttributes,
),
);
}

ユーザー属性を取得する

fetchUserAttributes APIを使用してユーザーのユーザー属性を取得し、プロファイルで読み取ることができます。これはフロントエンドエクスペリエンスをパーソナライズし、ユーザーが見るコンテンツを制御するのに役立ちます。

Future<void> fetchCurrentUserAttributes() async {
try {
final result = await Amplify.Auth.fetchUserAttributes();
for (final element in result) {
safePrint('key: ${element.userAttributeKey}; value: ${element.value}');
}
} on AuthException catch (e) {
safePrint('Error fetching user attributes: ${e.message}');
}
}

ユーザー属性を更新する

updateUserAttribute APIを使用して、新しいユーザー属性を作成するか、既存のユーザー属性を更新できます。

Future<void> updateUserEmail({
required String newEmail,
}) async {
try {
final result = await Amplify.Auth.updateUserAttribute(
userAttributeKey: AuthUserAttributeKey.email,
value: newEmail,
);
_handleUpdateUserAttributeResult(result);
} on AuthException catch (e) {
safePrint('Error updating user attribute: ${e.message}');
}
}

ユーザー属性の更新は、完了する前に追加の確認が必要な場合があります。Amplify.Auth.updateUserAttributeから返されたUpdateUserAttributeResultをチェックして、必要な次のステップを確認してください。更新が完了すると、次のステップはdoneになります。

void _handleUpdateUserAttributeResult(
UpdateUserAttributeResult result,
) {
switch (result.nextStep.updateAttributeStep) {
case AuthUpdateAttributeStep.confirmAttributeWithCode:
final codeDeliveryDetails = result.nextStep.codeDeliveryDetails!;
_handleCodeDelivery(codeDeliveryDetails);
break;
case AuthUpdateAttributeStep.done:
safePrint('Successfully updated attribute');
break;
}
}
void _handleCodeDelivery(AuthCodeDeliveryDetails codeDeliveryDetails) {
safePrint(
'A confirmation code has been sent to ${codeDeliveryDetails.destination}. '
'Please check your ${codeDeliveryDetails.deliveryMedium.name} for the code.',
);
}

複数のユーザー属性を一度に更新するには、updateUserAttributesを呼び出します。

Future<void> updateUserAttributes() async {
const attributes = [
AuthUserAttribute(
userAttributeKey: AuthUserAttributeKey.email,
value: 'email@email.com',
),
AuthUserAttribute(
userAttributeKey: AuthUserAttributeKey.familyName,
value: 'MyFamilyName',
),
];
try {
final result = await Amplify.Auth.updateUserAttributes(
attributes: attributes,
);
result.forEach((key, value) {
switch (value.nextStep.updateAttributeStep) {
case AuthUpdateAttributeStep.confirmAttributeWithCode:
final destination = value.nextStep.codeDeliveryDetails?.destination;
safePrint('Confirmation code sent to $destination for $key');
break;
case AuthUpdateAttributeStep.done:
safePrint('Update completed for $key');
break;
}
});
} on AuthException catch (e) {
safePrint('Error updating user attributes: ${e.message}');
}
}

ユーザー属性を確認する

一部の属性は、属性更新を完了するために確認が必要です。属性の確認が必要な場合、updateUserAttributeまたはupdateUserAttributes APIの結果の一部はCONFIRM_ATTRIBUTE_WITH_CODEになります。確認コードは、配信の詳細で言及された配信媒体に送信されます。ユーザーが確認コードを取得したら、ユーザーがコードを入力するための UIを提示し、ユーザーの入力を使用してconfirmUserAttribute APIを呼び出すことができます。

Future<void> verifyAttributeUpdate() async {
try {
await Amplify.Auth.confirmUserAttribute(
userAttributeKey: AuthUserAttributeKey.email,
confirmationCode: '390739',
);
} on AuthException catch (e) {
safePrint('Error confirming attribute update: ${e.message}');
}
}

ユーザー属性検証コードを送信する

ユーザーが認証されている間に属性を検証する必要がある場合は、以下に示すようにsendUserAttributeVerificationCode APIを呼び出します。

Future<void> resendVerificationCode() async {
try {
final result = await Amplify.Auth.resendUserAttributeConfirmationCode(
userAttributeKey: AuthUserAttributeKey.email,
);
_handleCodeDelivery(result.codeDeliveryDetails);
} on AuthException catch (e) {
safePrint('Error resending code: ${e.message}');
}
}

次のステップ