Manage user attributes
User attributes such as email address, phone number help you identify individual users. Defining the user attributes you include for your user profiles makes user data easy to manage at scale. This information will help you personalize user journeys, tailor content, provide intuitive account control, and more. You can capture information upfront during sign-up or enable customers to update their profile after sign-up. In this section we take a closer look at working with user attributes, how to set them up and manage them.
Retrieve user attributes
You can retrieve user attributes for your users to read in their profile using the fetchUserAttributes
API. This helps you personalize their frontend experience as well as control what they will see.
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) );
Update user attribute
You can use the updateUserAttribute
API to create or update existing user attributes.
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) );
Update user attributes
You can use the updateUserAttributes
API to create or update multiple existing user attributes.
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) );
Verify user attribute
Some attributes require confirmation for the attribute update to complete. If the attribute needs to be confirmed, part of the result of the updateUserAttribute
or updateUserAttributes
APIs will be CONFIRM_ATTRIBUTE_WITH_CODE
. A confirmation code will be sent to the delivery medium mentioned in the delivery details. When the user gets the confirmation code, you can present a UI to the user to enter the code and invoke the confirmUserAttribute
API with their input:
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) );
Send user attribute verification code
If an attribute needs to be verified while the user is authenticated, invoke the sendUserAttributeVerificationCode
API as shown below:
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) );