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.

1Future<void> fetchCurrentUserAttributes() async {
2 try {
3 final result = await Amplify.Auth.fetchUserAttributes();
4 for (final element in result) {
5 safePrint('key: ${element.userAttributeKey}; value: ${element.value}');
6 }
7 } on AuthException catch (e) {
8 safePrint('Error fetching user attributes: ${e.message}');
9 }
10}

Update user attribute

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

To update a single user attribute, call updateUserAttribute:

1Future<void> updateUserEmail({
2 required String newEmail,
3}) async {
4 try {
5 final result = await Amplify.Auth.updateUserAttribute(
6 userAttributeKey: AuthUserAttributeKey.email,
7 value: newEmail,
8 );
9 _handleUpdateUserAttributeResult(result);
10 } on AuthException catch (e) {
11 safePrint('Error updating user attribute: ${e.message}');
12 }
13}

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.

1void _handleUpdateUserAttributeResult(
2 UpdateUserAttributeResult result,
3) {
4 switch (result.nextStep.updateAttributeStep) {
5 case AuthUpdateAttributeStep.confirmAttributeWithCode:
6 final codeDeliveryDetails = result.nextStep.codeDeliveryDetails!;
7 _handleCodeDelivery(codeDeliveryDetails);
8 break;
9 case AuthUpdateAttributeStep.done:
10 safePrint('Successfully updated attribute');
11 break;
12 }
13}
14
15void _handleCodeDelivery(AuthCodeDeliveryDetails codeDeliveryDetails) {
16 safePrint(
17 'A confirmation code has been sent to ${codeDeliveryDetails.destination}. '
18 'Please check your ${codeDeliveryDetails.deliveryMedium.name} for the code.',
19 );
20}

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

1Future<void> updateUserAttributes() async {
2 const attributes = [
3 AuthUserAttribute(
4 userAttributeKey: AuthUserAttributeKey.email,
5 value: 'email@email.com',
6 ),
7 AuthUserAttribute(
8 userAttributeKey: AuthUserAttributeKey.familyName,
9 value: 'MyFamilyName',
10 ),
11 ];
12 try {
13 final result = await Amplify.Auth.updateUserAttributes(
14 attributes: attributes,
15 );
16 result.forEach((key, value) {
17 switch (value.nextStep.updateAttributeStep) {
18 case AuthUpdateAttributeStep.confirmAttributeWithCode:
19 final destination = value.nextStep.codeDeliveryDetails?.destination;
20 safePrint('Confirmation code sent to $destination for $key');
21 break;
22 case AuthUpdateAttributeStep.done:
23 safePrint('Update completed for $key');
24 break;
25 }
26 });
27 } on AuthException catch (e) {
28 safePrint('Error updating user attributes: ${e.message}');
29 }
30}

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:

1Future<void> verifyAttributeUpdate() async {
2 try {
3 await Amplify.Auth.confirmUserAttribute(
4 userAttributeKey: AuthUserAttributeKey.email,
5 confirmationCode: '390739',
6 );
7 } on AuthException catch (e) {
8 safePrint('Error confirming attribute update: ${e.message}');
9 }
10}

Resend verification code

If the code has expired or the user needs to resend the confirmation code, invoke the resend api as shown below:

1Future<void> resendVerificationCode() async {
2 try {
3 final result = await Amplify.Auth.resendUserAttributeConfirmationCode(
4 userAttributeKey: AuthUserAttributeKey.email,
5 );
6 _handleCodeDelivery(result.codeDeliveryDetails);
7 } on AuthException catch (e) {
8 safePrint('Error resending code: ${e.message}');
9 }
10}

Custom attributes

Amplify Flutter supports standard OIDC user attributes as well as custom attributes. Custom attributes can be instantiated via the custom attribute constructor:

1Future<void> _signUp({
2 required String username,
3 required String password,
4 required String email,
5 required String customValue,
6}) async {
7 final userAttributes = {
8 AuthUserAttributeKey.email: email,
9 // Create and pass a custom attribute
10 const CognitoUserAttributeKey.custom('my-custom-attribute'): customValue
11 };
12 await Amplify.Auth.signUp(
13 username: username,
14 password: password,
15 options: SignUpOptions(
16 userAttributes: userAttributes,
17 ),
18 );
19}

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.