Page updated Jan 16, 2024

Manage user profiles

User profile management helps you keep your applications secure while also personalizing your app experiences. In this guide we will review how you can enable your users to personalize their profile and verify their contact information. This includes outlining how you can set up user attributes, verify them, and allow your users to delete them when necessary.

Before you begin, you will need:

  • An Amplify project with the Auth category configured
  • The Amplify libraries installed and configured

Set up user attributes

User attributes such as email address, phone number help you identify individual users. Defining the user attributes you include for your user profiles makes user data easy to manage at scale. This information will help you personalize user journeys, tailor content, provide intuitive account control, and more. You can capture information upfront during sign-up or enable customers to update their profile after sign-up. In this section we take a closer look at working with user attributes and how to set them up and manage them.

Enable standard attributes

Amazon Cognito has a set of default standard attributes. To configure and enable standard user attributes using the Amplify CLI in your app, you can run the Amplify amplify add auth command and choose Walkthrough all the auth configurations. If you have previously created your auth resources, you can instead run the amplify update auth command in your terminal. When prompted for Specify read attributes and Specify write attributes, choose the attributes you'd like to enable in your app.

Learn more
Review standard attributes options

There are many user attributes available to use by default in Cognito. Their definitions are based on the OpenID Connect specification:

  • address
  • birthdate
  • email
  • family_name
  • gender
  • given_name
  • locale
  • middle_name
  • name
  • nickname
  • phone_number
  • picture
  • preferred_username
  • profile
  • zoneinfo
  • updated_at
  • website

You can find a list of all the standard attributes here.

Pass user attributes during sign-up

You can create user attributes during sign-up or when the user is authenticated. To do this as part of sign-up you can pass them in the userAttributes object of the signUp API:

1import { signUp } from 'aws-amplify/auth';
2
3async function handleSignUp() {
4 try {
5 await signUp({
6 username: 'jdoe',
7 password: 'mysecurerandompassword#123',
8 options: {
9 userAttributes: {
10 email: 'me@domain.com',
11 phone_number: '+12128601234', // E.164 number convention
12 given_name: 'Jane',
13 family_name: 'Doe',
14 nickname: 'Jane'
15 }
16 }
17 });
18 } catch (e) {
19 console.log(e);
20 }
21}

Configure custom user attributes

Custom attributes can be passed in with the userAttributes option of the signUp API:

1signUp({
2 username: 'jdoe',
3 password: 'mysecurerandompassword#123',
4 options: {
5 userAttributes: {
6 'custom:attribute_name_1': 'attribute_value_1',
7 'custom:attribute_name_2': 'attribute_value_2',
8 'custom:attribute_name_3': 'attribute_value_3'
9 }
10 }
11});

Retrieve user attributes

You can then retrieve user attributes for your users to read in their profile. This helps you personalize their frontend experience as well as control what they will see.

You can retrieve a user's latest attributes using the fetchUserAttributes API.

1import { fetchUserAttributes } from 'aws-amplify/auth';
2
3async function handleFetchUserAttributes() {
4 try {
5 const userAttributes = await fetchUserAttributes();
6 console.log(userAttributes);
7 } catch (error) {
8 console.log(error);
9 }
10}

Enable users to update, verify, and delete specific attributes

You can enable your users to update, verify, and delete specific user attributes as their information changes over time. These attributes are enabled as writable when you update the Specify write attributes in your Auth configuration.

Update user attribute

Invoke the updateUserAttribute API to create or update existing user attributes.

1import {
2 updateUserAttribute,
3 type UpdateUserAttributeOutput
4} from 'aws-amplify/auth';
5
6async function handleUpdateUserAttribute(attributeKey: string, value: string) {
7 try {
8 const output = await updateUserAttribute({
9 userAttribute: {
10 attributeKey,
11 value
12 }
13 });
14 handleUpdateUserAttributeNextSteps(output);
15 } catch (error) {
16 console.log(error);
17 }
18}
19
20function handleUpdateUserAttributeNextSteps(output: UpdateUserAttributeOutput) {
21 const { nextStep } = output;
22
23 switch (nextStep.updateAttributeStep) {
24 case 'CONFIRM_ATTRIBUTE_WITH_CODE':
25 const codeDeliveryDetails = nextStep.codeDeliveryDetails;
26 console.log(
27 `Confirmation code was sent to ${codeDeliveryDetails?.deliveryMedium}.`
28 );
29 // Collect the confirmation code from the user and pass to confirmUserAttribute.
30 break;
31 case 'DONE':
32 console.log(`attribute was successfully updated.`);
33 break;
34 }
35}
1import { updateUserAttribute } from 'aws-amplify/auth';
2
3async function handleUpdateUserAttribute(attributeKey, value) {
4 try {
5 const output = await updateUserAttribute({
6 userAttribute: {
7 attributeKey,
8 value
9 }
10 });
11 handleUpdateUserAttributeNextSteps(output);
12 } catch (error) {
13 console.log(error);
14 }
15}
16
17function handleUpdateUserAttributeNextSteps(output) {
18 const { nextStep } = output;
19
20 switch (nextStep.updateAttributeStep) {
21 case 'CONFIRM_ATTRIBUTE_WITH_CODE':
22 const codeDeliveryDetails = nextStep.codeDeliveryDetails;
23 console.log(
24 `Confirmation code was sent to ${codeDeliveryDetails?.deliveryMedium}.`
25 );
26 // Collect the confirmation code from the user and pass to confirmUserAttribute.
27 break;
28 case 'DONE':
29 console.log(`attribute was successfully updated.`);
30 break;
31 }
32}

Note: If you change an attribute that requires confirmation (i.e. email or phone_number), the user will receive a confirmation code either to their email or cellphone. This code can be used with the confirmUserAttribute API to confirm the change.

Update user attributes

Invoke the updateUserAttributes API to create or update multiple existing user attributes.

1import {
2 updateUserAttributes,
3 type UpdateUserAttributesOutput
4} from 'aws-amplify/auth';
5
6async function handleUpdateEmailAndNameAttributes(
7 updatedEmail: string,
8 updatedName: string
9) {
10 try {
11 const attributes = await updateUserAttributes({
12 userAttributes: {
13 email: updatedEmail,
14 name: updatedName
15 }
16 });
17 // handle next steps
18 } catch (error) {
19 console.log(error);
20 }
21}
1import { updateUserAttributes, type UpdateUserAttributesOutput } from 'aws-amplify/auth';
2
3async function handleUpdateEmailAndNameAttributes(
4 updatedEmail,
5 updatedName
6) {
7 try {
8 const attributes = await updateUserAttributes({
9 userAttributes: {
10 email: updatedEmail,
11 name: updatedName,
12 },
13 });
14 // handle next steps
15 } catch (error) {
16 console.log(error);
17 }
18}

Verify user attribute

Some attributes require confirmation for the attribute update to complete. If the attribute needs to be confirmed, part of the result of the updateUserAttribute or updateUserAttributes APIs 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 confirmUserAttribute API with their input:

1import {
2 confirmUserAttribute,
3 type ConfirmUserAttributeInput
4} from 'aws-amplify/auth';
5
6async function handleConfirmUserAttribute({
7 userAttributeKey,
8 confirmationCode
9}: ConfirmUserAttributeInput) {
10 try {
11 await confirmUserAttribute({ userAttributeKey, confirmationCode });
12 } catch (error) {
13 console.log(error);
14 }
15}
1import { confirmUserAttribute } from 'aws-amplify/auth';
2
3async function handleConfirmUserAttribute({
4 userAttributeKey,
5 confirmationCode
6}) {
7 try {
8 await confirmUserAttribute({ userAttributeKey, confirmationCode });
9 } catch (error) {
10 console.log(error);
11 }
12}

Send user attribute verification code

If an attribute needs to be verified while the user is authenticated, invoke the sendUserAttributeVerificationCode API as shown below:

1import {
2 sendUserAttributeVerificationCode,
3 type VerifiableUserAttributeKey
4} from 'aws-amplify/auth';
5
6async function handleSendUserAttributeVerificationCode(
7 key: VerifiableUserAttributeKey
8) {
9 try {
10 await sendUserAttributeVerificationCode({
11 userAttributeKey: key
12 });
13 } catch (error) {
14 console.log(error);
15 }
16}
1import { sendUserAttributeVerificationCode } from 'aws-amplify/auth';
2
3async function handleSendUserAttributeVerificationCode(key) {
4 try {
5 await sendUserAttributeVerificationCode({
6 userAttributeKey: key
7 });
8 } catch (error) {
9 console.log(error);
10 }
11}

Delete user attributes

The deleteUserAttributes API allows to delete one or more user attributes.

1import {
2 deleteUserAttributes,
3 type DeleteUserAttributesInput
4} from 'aws-amplify/auth';
5
6async function handleDeleteUserAttributes(
7 keys: DeleteUserAttributesInput['userAttributeKeys']
8) {
9 try {
10 await deleteUserAttributes({
11 userAttributeKeys: ['custom:my_custom_attribute', ...keys]
12 });
13 } catch (error) {
14 console.log(error);
15 }
16}
1import { deleteUserAttributes } from 'aws-amplify/auth';
2
3async function handleDeleteUserAttributes(keys) {
4 try {
5 await deleteUserAttributes({
6 userAttributeKeys: ['custom:my_custom_attribute', ...keys]
7 });
8 } catch (error) {
9 console.log(error);
10 }
11}

By following the above steps your users can now update, verify, and delete specific user attributes as their information changes over time.

Conclusion

Congratulations! You finished the Manage user profiles guide and your users can now review and customize their profile information.

Next steps

Now that you completed setting up user profile management, you may also want to add some additional features. We recommend: