# 4.2.1 Using user pools with AWS JavaScript SDK

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](https://bayun.gitbook.io/bayun-awss3-wrapper-javascript-programming-guide/6.-bayuns3#aws-credentials) to know more about window.\_config file.

{% tabs %}
{% tab title="JavaScript" %}
```javascript
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);
```
{% endtab %}
{% endtabs %}

### Register a User

Use `userPool.signUp` method to sign up a user.

{% tabs %}
{% tab title="JavaScript" %}
```javascript
//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());
});
```
{% endtab %}
{% endtabs %}

### Confirm Signup

Confirm a user's sign up with the confirmation code which is sent on user's email address.

{% tabs %}
{% tab title="JavaScript" %}
```javascript
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
        }        
     });
}
```
{% endtab %}
{% endtabs %}

### Sign in a User

Use `cognitoUser.authenticateUser` method to get a session with the username and password.

{% tabs %}
{% tab title="JavaScript" %}
```javascript
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));
        },
});

```
{% endtab %}
{% endtabs %}

### Sign out a user

Use `cognitoUser.signOut` method to log a user out.

{% tabs %}
{% tab title="JavaScript" %}
```javascript
cognitoUser.signOut();
```
{% endtab %}
{% endtabs %}

