Manage user attributes
Fetch the current user's attributes
Invoke the following api to get the list of attributes assigned to the user.
1Amplify.Auth.fetchUserAttributes(2 attributes -> Log.i("AuthDemo", "User attributes = " + attributes.toString()),3 error -> Log.e("AuthDemo", "Failed to fetch user attributes.", error)4);
1Amplify.Auth.fetchUserAttributes(2 { Log.i("AuthDemo", "User attributes = $attributes") },3 { Log.e("AuthDemo", "Failed to fetch user attributes", it) }4)
1try {2 val attributes = Amplify.Auth.fetchUserAttributes()3 Log.i("AuthDemo", "User attributes = $attributes")4} catch (error: AuthException) {5 Log.e("AuthDemo", "Failed to fetch user attributes", error)6}
1RxAmplify.Auth.fetchUserAttributes()2 .doOnSubscribe(() -> Log.i("AuthDemo", "Attributes:"))3 .flatMapObservable(Observable::fromIterable)4 .subscribe(5 eachAttribute -> Log.i("AuthDemo", eachAttribute.toString()),6 error -> Log.e("AuthDemo", "Failed to fetch attributes.", error)7 );
Update user attribute
Invoke the update api for creating new or updating existing user attributes.
To update a single user attribute, call updateUserAttribute
:
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);
1Amplify.Auth.updateUserAttribute(2 AuthUserAttribute(AuthUserAttributeKey.email(), "email@email.com"),3 { Log.i("AuthDemo", "Updated user attribute = $it") },4 { Log.e("AuthDemo", "Failed to update user attribute.", it) }5)
1val attribute =2 AuthUserAttribute(AuthUserAttributeKey.email(), "email@email.com")3try {4 val result = Amplify.Auth.updateUserAttribute(attribute)5 Log.i("AuthDemo", "Updated user attribute = $result")6} catch (error: AuthException) {7 Log.e("AuthDemo", "Failed to update user attribute.", error)8}
1AuthUserAttribute userEmail =2 new AuthUserAttribute(AuthUserAttributeKey.email(), "email@email.com");3RxAmplify.Auth.updateUserAttribute(userEmail)4 .subscribe(5 result -> Log.i("AuthDemo", "Updated user attribute = " + result.toString()),6 error -> Log.e("AuthDemo", "Failed to update user attribute.", error)7 );
To update multiple user attributes at a time, call updateUserAttributes
:
1Amplify.Auth.updateUserAttributes(2 attributes, // attributes is a list of AuthUserAttribute3 result -> Log.i("AuthDemo", "Updated user attributes = " + result.toString()),4 error -> Log.e("AuthDemo", "Failed to update user attributes.", error)5);
1Amplify.Auth.updateUserAttributes(2 attributes, // attributes is a list of AuthUserAttribute3 { Log.i("AuthDemo", "Updated user attributes = $it") },4 { Log.e("AuthDemo", "Failed to update user attributes", it) }5)
1try {2 val result = Amplify.Auth.updateUserAttributes(attributes)3 Log.i("AuthDemo", "Updated user attributes = $result")4} catch (error: AuthException) {5 Log.e("AuthDemo", "Failed to update user attributes", error)6}
1// attributes is a list of AuthUserAttribute2RxAmplify.Auth.updateUserAttributes(attributes)3 .subscribe(4 result -> Log.i("AuthDemo", "Updated user attributes = " + result.toString()),5 error -> Log.e("AuthDemo", "Failed to update user attributes.", error)6 );
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:
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);
1Amplify.Auth.confirmUserAttribute(AuthUserAttributeKey.email(), "344299",2 { Log.i("AuthDemo", "Confirmed user attribute with correct code.") },3 { Log.e("AuthDemo", "Failed to confirm user attribute. Bad code?", it) }4)
1try {2 Amplify.Auth.confirmUserAttribute(AuthUserAttributeKey.email(), "344299")3 Log.i("AuthDemo", "Confirmed user attribute with correct code.") 4} catch (error: AuthException) {5 Log.e("AuthDemo", "Failed to confirm user attribute. Bade code?", error) 6}
1RxAmplify.Auth.confirmUserAttribute(AuthUserAttributeKey.email(), "344299")2 .subscribe(3 () -> Log.i("AuthDemo", "Confirmed user attribute using correct code."),4 error -> Log.e("AuthDemo", "Failed to confirm user attribute. Bad code?", error)5 );
Resend verification code
If the code has expired or the user needs to resend the confirmation code, invoke the resend api as shown below:
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);
1Amplify.Auth.resendUserAttributeConfirmationCode(2 AuthUserAttributeKey.email(),3 { Log.i("AuthDemo", "Code was sent again: $it") },4 { Log.e("AuthDemo", "Failed to resend code", it) }5)
1try {2 val attr = AuthUserAttributeKey.email()3 val result = Amplify.Auth.resendUserAttributeConfirmationCode(attr)4 Log.i("AuthDemo", "Code was sent again: $result."),5} catch (error: AuthException) {6 Log.e("AuthDemo", "Failed to resend code.", error)7}
1RxAmplify.Auth.resendUserAttributeConfirmationCode(AuthUserAttributeKey.email())2 .subscribe(3 result -> Log.i("AuthDemo", "Code was resent: " + result.toString()),4 error -> Log.e("AuthDemo", "Failed to resend code.", error)5 );