Page updated Nov 14, 2023

Manage user attributes

Fetch the current user's attributes

Invoke the following api to get the list of attributes assigned to the user.

Amplify.Auth.fetchUserAttributes( attributes -> Log.i("AuthDemo", "User attributes = " + attributes.toString()), error -> Log.e("AuthDemo", "Failed to fetch user attributes.", error) );
1Amplify.Auth.fetchUserAttributes(
2 attributes -> Log.i("AuthDemo", "User attributes = " + attributes.toString()),
3 error -> Log.e("AuthDemo", "Failed to fetch user attributes.", error)
4);

Update user attribute

Invoke the update api for creating new or updating existing user attributes.

To update a single user attribute, call updateUserAttribute:

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) );
1AuthUserAttribute userEmail =
2 new AuthUserAttribute(AuthUserAttributeKey.email(), "email@email.com");
3Amplify.Auth.updateUserAttribute(userEmail,
4 result -> Log.i("AuthDemo", "Updated user attribute = " + result.toString()),
5 error -> Log.e("AuthDemo", "Failed to update user attribute.", error)
6);

To update multiple user attributes at a time, call updateUserAttributes:

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) );
1Amplify.Auth.updateUserAttributes(
2 attributes, // attributes is a list of AuthUserAttribute
3 result -> Log.i("AuthDemo", "Updated user attributes = " + result.toString()),
4 error -> Log.e("AuthDemo", "Failed to update user attributes.", error)
5);

Verify user attribute

Some attributes require confirmation for the attribute update to complete. If the attribute need to be confirmed, the result of the above api 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 confirm attribute 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) );
1Amplify.Auth.confirmUserAttribute(AuthUserAttributeKey.email(), "344299",
2 () -> Log.i("AuthDemo", "Confirmed user attribute with correct code."),
3 error -> Log.e("AuthDemo", "Failed to confirm user attribute. Bad code?", error)
4);

Resend verification code

If the code has expired or the user needs to resend the confirmation code, invoke the resend 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) );
1Amplify.Auth.resendUserAttributeConfirmationCode(AuthUserAttributeKey.email(),
2 result -> Log.i("AuthDemo", "Code was sent again: " + result.toString()),
3 error -> Log.e("AuthDemo", "Failed to resend code.", error)
4);