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 :
- Introduction
- What is Amazon Cognito ?
- Maven dependency
- Add or Modify phone for Cognito user
- Add or Modify email for Cognito user
- Questions related to AWS Cognito Change User Email or Phone Using Java
- Summary
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;
}
Articles/Questions related to AWS Cognito Change User Email or Phone
using Java
Create a Cognito user pool in AWS Console | Set up an Amazon Cognito user pool AWS Cognito Confirming User Accounts using Java
AWS Cognito SignUp and SignIn Example Using Java
AWS Cognito TOTP Software Token MFA Using Java
AWS Cognito Change User Email for Phone using Java
AWS Cognito Reset User MFA Using Java
AWS Cognito Enable SMS MFA Using Java
How to list all Amazon Cognito Users using Java
AWSCognitoIdentityProvider Method Example for Cognito User Pools API
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.
0 Comments
Post a Comment