Amazon Cognito provides authentication, authorization, and user management for your web and mobile apps. Your users can sign in directly with a username and password, or through a third party such as Facebook, Amazon, Google, or Apple.

We can configure Amazon Cognito in such a way that users can sign in with their email, phone, or user name. If you enable sign-in with a user name, you can set a "preferred user name" attribute (preferred_username) which will work as an alternate value for the username attribute (username).

In AWS Cognito when a user changes his or her email address or phone number in your app, that attribute is marked as unverified. If auto-verification was enabled for the attribute being updated, the service immediately sends the user a message containing a verification code, which the user should enter to verify the change.

But here we added a new phone and email and also verified it using the admin request.

Table of Content :

Maven dependency

<dependency> <groupId>com.amazonaws</groupId> <artifactId>aws-java-sdk-core</artifactId> <version>1.11.764</version> </dependency> <dependency> <groupId>com.amazonaws</groupId> <artifactId>aws-java-sdk-cognitoidp</artifactId> <version>1.11.764</version> </dependency> <dependency> <groupId>com.amazonaws</groupId> <artifactId>aws-java-sdk</artifactId> <version>1.11.360</version> </dependency>

Add or Modify phone for Cognito user

public static boolean addOrModifyRegisteredPhoneForUser(String username, String phone) { try { final AWSCognitoIdentityProvider client = getAWSCognitoIdentityClient(); List < AttributeType > userAttributes = new ArrayList < AttributeType >(); AttributeType userAttributeEmail = new AttributeType().withName("phone_number").withValue(phone); AttributeType userAttributeEmailVerified = new AttributeType().withName("phone_number_verified").withValue("true"); userAttributes.add(userAttributeEmail); userAttributes.add(userAttributeEmailVerified); AdminUpdateUserAttributesRequest adminUpdateUserAttributesRequest = new AdminUpdateUserAttributesRequest(). withUsername(username).withUserPoolId(userPoolId). withUserAttributes(userAttributes); AdminUpdateUserAttributesResult result = provider.adminUpdateUserAttributes(adminUpdateUserAttributesRequest); System.out.println("Phone Modified successfully"); return Boolean.TRUE; } catch(Exception ex) { System.out.println("Error occurred during changing the phone for the cognito user." + ex); } return Boolean.FALSE; }

Add or Modify email for Cognito user

public static boolean addOrModifyRegisteredEmail(String username, String email) { try { final AWSCognitoIdentityProvider client = getAWSCognitoIdentityClient(); List< AttributeType > userAttributes = new ArrayList< AttributeType > (); AttributeType emailAttribute = new AttributeType().withName("email").withValue(email); AttributeType emailVerifiedAttribute = new AttributeType().withName("email_verified").withValue("true"); userAttributes.add(emailAttribute); userAttributes.add(emailVerifiedAttribute); AdminUpdateUserAttributesRequest adminUpdateUserAttributesRequest = new AdminUpdateUserAttributesRequest(). withUsername(username).withUserPoolId(userPoolId). withUserAttributes(userAttributes); AdminUpdateUserAttributesResult result = provider.adminUpdateUserAttributes(adminUpdateUserAttributesRequest); System.out.println("Email Modified successfully"); return Boolean.TRUE; } catch(Exception ex) { System.out.println("Error occurred during changing the email for the cognito user." + ex); } return Boolean.FALSE; }

In this article, we have seen AWS Cognito Change User Email or Phone using Java. All source code in the article can be found in the GitHub repository.