Manage user attributes
Fetch the current user's attributes
Invoke the following api to get the list of attributes assigned to the user.
Future<void> fetchCurrentUserAttributes() async { try { final result = await Amplify.Auth.fetchUserAttributes(); for (final element in result) { safePrint('key: ${element.userAttributeKey}; value: ${element.value}'); } } on AuthException catch (e) { safePrint('Error fetching user attributes: ${e.message}'); }}
Update user attribute
Invoke the update api for creating new or updating existing user attributes.
To update a single user attribute, call updateUserAttribute
:
Future<void> updateUserEmail({ required String newEmail,}) async { try { final result = await Amplify.Auth.updateUserAttribute( userAttributeKey: AuthUserAttributeKey.email, value: newEmail, ); _handleUpdateUserAttributeResult(result); } on AuthException catch (e) { safePrint('Error updating user attribute: ${e.message}'); }}
User attribute updates may require additional verification before they're complete. Check the
UpdateUserAttributeResult
returned from Amplify.Auth.updateUserAttribute
to see which next
step, if any, is required. When the update is complete, the next step will be done
.
void _handleUpdateUserAttributeResult( UpdateUserAttributeResult result,) { switch (result.nextStep.updateAttributeStep) { case AuthUpdateAttributeStep.confirmAttributeWithCode: final codeDeliveryDetails = result.nextStep.codeDeliveryDetails!; _handleCodeDelivery(codeDeliveryDetails); break; case AuthUpdateAttributeStep.done: safePrint('Successfully updated attribute'); break; }}
void _handleCodeDelivery(AuthCodeDeliveryDetails codeDeliveryDetails) { safePrint( 'A confirmation code has been sent to ${codeDeliveryDetails.destination}. ' 'Please check your ${codeDeliveryDetails.deliveryMedium.name} for the code.', );}
To update multiple user attributes at a time, call updateUserAttributes
:
Future<void> updateUserAttributes() async { const attributes = [ AuthUserAttribute( userAttributeKey: AuthUserAttributeKey.email, value: 'email@email.com', ), AuthUserAttribute( userAttributeKey: AuthUserAttributeKey.familyName, value: 'MyFamilyName', ), ]; try { final result = await Amplify.Auth.updateUserAttributes( attributes: attributes, ); result.forEach((key, value) { switch (value.nextStep.updateAttributeStep) { case AuthUpdateAttributeStep.confirmAttributeWithCode: final destination = value.nextStep.codeDeliveryDetails?.destination; safePrint('Confirmation code sent to $destination for $key'); break; case AuthUpdateAttributeStep.done: safePrint('Update completed for $key'); break; } }); } on AuthException catch (e) { safePrint('Error updating user attributes: ${e.message}'); }}
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:
Future<void> verifyAttributeUpdate() async { try { await Amplify.Auth.confirmUserAttribute( userAttributeKey: AuthUserAttributeKey.email, confirmationCode: '390739', ); } on AuthException catch (e) { safePrint('Error confirming attribute update: ${e.message}'); }}
Send user attribute verification code
If an attribute needs to be verified while the user is authenticated, invoke the api as shown below:
Future<void> resendVerificationCode() async { try { final result = await Amplify.Auth.sendUserAttributeConfirmationCode( userAttributeKey: AuthUserAttributeKey.email, ); _handleCodeDelivery(result.codeDeliveryDetails); } on AuthException catch (e) { safePrint('Error resending code: ${e.message}'); }}
Custom attributes
Amplify Flutter supports standard OIDC user attributes as well as custom attributes. Custom attributes can be instantiated via the custom attribute constructor:
Future<void> signUp({ required String username, required String password, required String email, required String customValue,}) async { final userAttributes = { AuthUserAttributeKey.email: email, // Create and pass a custom attribute const CognitoUserAttributeKey.custom('my-custom-attribute'): customValue }; await Amplify.Auth.signUp( username: username, password: password, options: SignUpOptions( userAttributes: userAttributes, ), );}
When working with a Cognito UserPool, you can set up custom attributes via the Cognito console or AWS CLI. Although Cognito prepends a "custom:" prefix on the attribute name, there is no need for you to add this in Amplify Flutter's custom attribute constructor.