ユーザー属性の管理
メールアドレスや電話番号などのユーザー属性は、個々のユーザーを識別するのに役立ちます。ユーザープロファイルに含めるユーザー属性を定義することで、ユーザーデータを大規模に管理しやすくなります。この情報は、ユーザージャーニーをパーソナライズし、コンテンツを調整し、直感的なアカウント管理を提供するなど、さまざまな用途に役立ちます。サインアップ時に事前に情報を収集することも、サインアップ後に顧客がプロファイルを更新できるようにすることもできます。このセクションでは、ユーザー属性の操作方法、設定方法、管理方法について詳しく説明します。
サインアップ時にユーザー属性を渡す
サインアップ時または認証されたユーザーのときにユーザー属性を作成できます。これをサインアップの一部として行うには、signUp APIのuserAttributesオブジェクトにユーザー属性を渡します。
import { signUp } from "aws-amplify/auth";
await signUp({ username: "jdoe", password: "mysecurerandompassword#123", options: { userAttributes: { email: "me@domain.com", phone_number: "+12128601234", // E.164 number convention given_name: "Jane", family_name: "Doe", nickname: "Jane", }, },});サインアップ時にカスタムユーザー属性を設定する
カスタム属性は、signUp APIのuserAttributesオプションで渡すことができます。
import { signUp } from "aws-amplify/auth";
await signUp({ username: 'john.doe@example.com', password: 'hunter2', options: { userAttributes: { 'custom:display_name': 'john_doe123', } }});ユーザー属性を取得する
fetchUserAttributes APIを使用してユーザーのユーザー属性を取得し、プロファイルで読み取ることができます。これはフロントエンドエクスペリエンスをパーソナライズし、ユーザーが見るコンテンツを制御するのに役立ちます。
import { fetchUserAttributes } from 'aws-amplify/auth';
await fetchUserAttributes();ユーザー属性を更新する
updateUserAttribute APIを使用して、新しいユーザー属性を作成するか、既存のユーザー属性を更新できます。
import { updateUserAttribute, type UpdateUserAttributeOutput} from 'aws-amplify/auth';
async function handleUpdateUserAttribute(attributeKey: string, value: string) { try { const output = await updateUserAttribute({ userAttribute: { attributeKey, value } }); handleUpdateUserAttributeNextSteps(output); } catch (error) { console.log(error); }}
function handleUpdateUserAttributeNextSteps(output: UpdateUserAttributeOutput) { const { nextStep } = output;
switch (nextStep.updateAttributeStep) { case 'CONFIRM_ATTRIBUTE_WITH_CODE': const codeDeliveryDetails = nextStep.codeDeliveryDetails; console.log( `Confirmation code was sent to ${codeDeliveryDetails?.deliveryMedium}.` ); // Collect the confirmation code from the user and pass to confirmUserAttribute. break; case 'DONE': console.log(`attribute was successfully updated.`); break; }}import { updateUserAttribute } from 'aws-amplify/auth';
async function handleUpdateUserAttribute(attributeKey, value) { try { const output = await updateUserAttribute({ userAttribute: { attributeKey, value } }); handleUpdateUserAttributeNextSteps(output); } catch (error) { console.log(error); }}
function handleUpdateUserAttributeNextSteps(output) { const { nextStep } = output;
switch (nextStep.updateAttributeStep) { case 'CONFIRM_ATTRIBUTE_WITH_CODE': const codeDeliveryDetails = nextStep.codeDeliveryDetails; console.log( `Confirmation code was sent to ${codeDeliveryDetails?.deliveryMedium}.` ); // Collect the confirmation code from the user and pass to confirmUserAttribute. break; case 'DONE': console.log(`attribute was successfully updated.`); break; }}ユーザー属性を更新する
updateUserAttributes APIを使用して、複数の既存ユーザー属性を作成または更新できます。
import { updateUserAttributes, type UpdateUserAttributesOutput } from "aws-amplify/auth";
await updateUserAttributes({ userAttributes: { email: "me@domain.com", name: "Jon Doe", },});ユーザー属性を確認する
一部の属性は、属性更新を完了するために確認が必要です。属性の確認が必要な場合、updateUserAttributeまたはupdateUserAttributes APIの結果の一部はCONFIRM_ATTRIBUTE_WITH_CODEになります。確認コードは、配信の詳細で言及された配信媒体に送信されます。ユーザーが確認コードを取得したら、ユーザーがコードを入力するための UIを提示し、ユーザーの入力を使用してconfirmUserAttribute APIを呼び出すことができます。
import { confirmUserAttribute, type ConfirmUserAttributeInput} from 'aws-amplify/auth';
async function handleConfirmUserAttribute({ userAttributeKey, confirmationCode}: ConfirmUserAttributeInput) { try { await confirmUserAttribute({ userAttributeKey, confirmationCode }); } catch (error) { console.log(error); }}ユーザー属性検証コードを送信する
ユーザーが認証されている間に属性を検証する必要がある場合は、以下に示すようにsendUserAttributeVerificationCode APIを呼び出します。
import { sendUserAttributeVerificationCode, type VerifiableUserAttributeKey} from 'aws-amplify/auth';
async function handleSendUserAttributeVerificationCode( key: VerifiableUserAttributeKey) { try { await sendUserAttributeVerificationCode({ userAttributeKey: key }); } catch (error) { console.log(error); }}ユーザー属性を削除する
deleteUserAttributes APIを使用すると、1つ以上のユーザー属性を削除できます。
import { deleteUserAttributes, type DeleteUserAttributesInput} from 'aws-amplify/auth';
async function handleDeleteUserAttributes( keys: DeleteUserAttributesInput['userAttributeKeys']) { try { await deleteUserAttributes({ userAttributeKeys: ['custom:my_custom_attribute', ...keys] }); } catch (error) { console.log(error); }}