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

Page updated Feb 21, 2024

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)
);
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

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)
);
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)
);

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)
);
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 AuthUserAttribute
RxAmplify.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 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)
);
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 send 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)
);