ユーザー属性の管理
メールアドレスや電話番号などのユーザー属性は、個々のユーザーを識別するのに役立ちます。ユーザープロファイルに含めるユーザー属性を定義することで、ユーザーデータを大規模に管理しやすくなります。この情報は、ユーザージャーニーをパーソナライズし、コンテンツを調整し、直感的なアカウント管理を提供するなど、さまざまな用途に役立ちます。サインアップ時に事前に情報を収集することも、サインアップ後に顧客がプロファイルを更新できるようにすることもできます。このセクションでは、ユーザー属性の操作方法、設定方法、管理方法について詳しく説明します。
ユーザー属性を取得する
fetchUserAttributes APIを使用してユーザーのユーザー属性を取得し、プロファイルで読み取ることができます。これはフロントエンドエクスペリエンスをパーソナライズし、ユーザーが見るコンテンツを制御するのに役立ちます。
Amplify.Auth.fetchUserAttributes( attributes -> Log.i("AuthDemo", "User attributes = " + attributes.toString()), error -> Log.e("AuthDemo", "Failed to fetch user attributes.", error));Amplify.Auth.fetchUserAttributes( { Log.i("AuthDemo", "User attributes = $it") }, { Log.e("AuthDemo", "Failed to fetch user attributes", it) })try { val attributes = Amplify.Auth.fetchUserAttributes() Log.i("AuthDemo", "User attributes = $attributes")} catch (error: AuthException) { Log.e("AuthDemo", "Failed to fetch user attributes", error)}RxAmplify.Auth.fetchUserAttributes() .doOnSubscribe(() -> Log.i("AuthDemo", "Attributes:")) .flatMapObservable(Observable::fromIterable) .subscribe( eachAttribute -> Log.i("AuthDemo", eachAttribute.toString()), error -> Log.e("AuthDemo", "Failed to fetch attributes.", error) );ユーザー属性を更新する
updateUserAttribute APIを使用して、新しいユーザー属性を作成するか、既存のユーザー属性を更新できます。
AuthUserAttribute userEmail = new AuthUserAttribute(AuthUserAttributeKey.email(), "email@email.com");Amplify.Auth.updateUserAttribute(userEmail, result -> Log.i("AuthDemo", "Updated user attribute = " + result.toString()), error -> Log.e("AuthDemo", "Failed to update user attribute.", error));Amplify.Auth.updateUserAttribute( AuthUserAttribute(AuthUserAttributeKey.email(), "email@email.com"), { Log.i("AuthDemo", "Updated user attribute = $it") }, { Log.e("AuthDemo", "Failed to update user attribute.", it) })val attribute = AuthUserAttribute(AuthUserAttributeKey.email(), "email@email.com")try { val result = Amplify.Auth.updateUserAttribute(attribute) Log.i("AuthDemo", "Updated user attribute = $result")} catch (error: AuthException) { Log.e("AuthDemo", "Failed to update user attribute.", error)}AuthUserAttribute userEmail = new AuthUserAttribute(AuthUserAttributeKey.email(), "email@email.com");RxAmplify.Auth.updateUserAttribute(userEmail) .subscribe( result -> Log.i("AuthDemo", "Updated user attribute = " + result.toString()), error -> Log.e("AuthDemo", "Failed to update user attribute.", error) );ユーザー属性を更新する
updateUserAttributes APIを使用して、複数の既存ユーザー属性を作成または更新できます。
Amplify.Auth.updateUserAttributes( attributes, // attributes is a list of AuthUserAttribute result -> Log.i("AuthDemo", "Updated user attributes = " + result.toString()), error -> Log.e("AuthDemo", "Failed to update user attributes.", error));Amplify.Auth.updateUserAttributes( attributes, // attributes is a list of AuthUserAttribute { Log.i("AuthDemo", "Updated user attributes = $it") }, { Log.e("AuthDemo", "Failed to update user attributes", it) })try { val result = Amplify.Auth.updateUserAttributes(attributes) Log.i("AuthDemo", "Updated user attributes = $result")} catch (error: AuthException) { Log.e("AuthDemo", "Failed to update user attributes", error)}// attributes is a list of AuthUserAttributeRxAmplify.Auth.updateUserAttributes(attributes) .subscribe( result -> Log.i("AuthDemo", "Updated user attributes = " + result.toString()), error -> Log.e("AuthDemo", "Failed to update user attributes.", error) );ユーザー属性を確認する
一部の属性は、属性更新を完了するために確認が必要です。属性の確認が必要な場合、updateUserAttributeまたはupdateUserAttributes APIの結果の一部はCONFIRM_ATTRIBUTE_WITH_CODEになります。確認コードは、配信の詳細で言及された配信媒体に送信されます。ユーザーが確認コードを取得したら、ユーザーがコードを入力するための UIを提示し、ユーザーの入力を使用してconfirmUserAttribute APIを呼び出すことができます。
Amplify.Auth.confirmUserAttribute(AuthUserAttributeKey.email(), "344299", () -> Log.i("AuthDemo", "Confirmed user attribute with correct code."), error -> Log.e("AuthDemo", "Failed to confirm user attribute. Bad code?", error));Amplify.Auth.confirmUserAttribute(AuthUserAttributeKey.email(), "344299", { Log.i("AuthDemo", "Confirmed user attribute with correct code.") }, { Log.e("AuthDemo", "Failed to confirm user attribute. Bad code?", it) })try { Amplify.Auth.confirmUserAttribute(AuthUserAttributeKey.email(), "344299") Log.i("AuthDemo", "Confirmed user attribute with correct code.") } catch (error: AuthException) { Log.e("AuthDemo", "Failed to confirm user attribute. Bade code?", error) }RxAmplify.Auth.confirmUserAttribute(AuthUserAttributeKey.email(), "344299") .subscribe( () -> Log.i("AuthDemo", "Confirmed user attribute using correct code."), error -> Log.e("AuthDemo", "Failed to confirm user attribute. Bad code?", error) );ユーザー属性検証コードを送信する
ユーザーが認証されている間に属性を検証する必要がある場合は、以下に示すようにsendUserAttributeVerificationCode APIを呼び出します。
Amplify.Auth.resendUserAttributeConfirmationCode(AuthUserAttributeKey.email(), result -> Log.i("AuthDemo", "Code was sent again: " + result.toString()), error -> Log.e("AuthDemo", "Failed to resend code.", error));Amplify.Auth.resendUserAttributeConfirmationCode( AuthUserAttributeKey.email(), { Log.i("AuthDemo", "Code was sent again: $it") }, { Log.e("AuthDemo", "Failed to resend code", it) })try { val attr = AuthUserAttributeKey.email() val result = Amplify.Auth.resendUserAttributeConfirmationCode(attr) Log.i("AuthDemo", "Code was sent again: $result."),} catch (error: AuthException) { Log.e("AuthDemo", "Failed to resend code.", error)}RxAmplify.Auth.resendUserAttributeConfirmationCode(AuthUserAttributeKey.email()) .subscribe( result -> Log.i("AuthDemo", "Code was resent: " + result.toString()), error -> Log.e("AuthDemo", "Failed to resend code.", error) );