Here are the details about registering, confirming, and authenticating users using standard AWS JavaScript SDK.
Creating an AWSCognitoIdentityUserPool Object
The following procedure describes how to create an AWSCognitoIdentityUserPool object to interact with.
Visit here to know more about window._config file.
let poolData = {
UserPoolId: "<userPoolId>", // Your user pool id here, you can also use window._config to hold you userPoolId.
ClientId: "<clientId>", // Your client id here, you can also use window._config to hold you clientId.
};
var userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);
Register a User
Use userPool.signUp method to sign up a user.
//attributeList is the list of parameters which are required for authentication,
//user has already deleared them while creating pool on AWS.
var attributeList = [];
var email = "<email>";
var personalname = "<personalname>";
var dataEmail = {
Name: 'email',
Value: email, //get from form field
};
var dataPersonalName = {
Name: 'name',
Value: personalname, //get from form field
};
var attributeEmail = new AmazonCognitoIdentity.CognitoUserAttribute(dataEmail);
var attributePersonalName = new AmazonCognitoIdentity.CognitoUserAttribute(dataPersonalName);
attributeList.push(attributeEmail);
attributeList.push(attributePersonalName);
userPool.signUp(username, password, attributeList, null, function (err, result) {
if (err) {
//error while signing up
console.log(err.message);
console.log("result", result);
alert(err.message || JSON.stringify(err));
return;
}
//user successfully signedup
cognitoUser = result.user;
console.log("User Details: ", cognitoUser);
console.log('user name is ' + cognitoUser.getUsername());
});
Confirm Signup
Confirm a user's sign up with the confirmation code which is sent on user's email address.
confirmSignUp = async(email, code) => {
//required parameters to verify a user
var params = {
ClientId: "<clientId>", /* required */
ConfirmationCode: String(code), /* required */
Username: String(email), /* required */
};
let region = "<region>";
var cognitoidentityserviceprovider = new AWS.CognitoIdentityServiceProvider({ apiVersion: '2022-12-20', region });
cognitoidentityserviceprovider.confirmSignUp(params, function (err, data) {
if (err) {console.log(err, err.stack);} // an error occurred
else {
console.log("Verification Success :" + data);
//successful response
}
});
}
Sign in a User
Use cognitoUser.authenticateUser method to get a session with the username and password.
var authenticationData = {
Username: "<email>",
Password: "<password>",
};
var authenticationDetails = new AmazonCognitoIdentity.AuthenticationDetails(authenticationData);
var poolData = {
UserPoolId: "<userPoolId>", // Your user pool id here
ClientId: "<clientId>", // Your client id here
};
var userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);
var userData = {
Username: "<email>",
Pool: userPool,
};
var cognitoUser = new AmazonCognitoIdentity.CognitoUser(userData);
cognitoUser.authenticateUser(authenticationDetails, {
onSuccess: function (result) {
var accessToken = result.getAccessToken().getJwtToken();
console.log("accessToken :", accessToken);
//cognitoUser.signOut();
},
onFailure: function (err) {
alert(err.message || JSON.stringify(err));
},
});