Showing posts with label cognito. Show all posts
Showing posts with label cognito. Show all posts

Jun 10, 2018

AWS Cognito SDK using JavaScript

Here is my first script for signup on AWS Cognito using Javascript SDK

AWS Cognito SDK:

https://github.com/amazon-archives/amazon-cognito-identity-js/tree/master/dist


Script:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
</head>
<body>
    <script type='text/javascript' src="aws-cognito-sdk.js"></script>
    <script type='text/javascript' src="amazon-cognito-identity.js"></script>
    <script>
        var data = {
            UserPoolId: 'us-east-XXXXXXXX',     // Insert your user pool id
            ClientId: 'XXXXXXXXXX' // Insert your app client id
        };
        var userPool = new AmazonCognitoIdentity.CognitoUserPool(data);
    </script>
    <fieldset>
        <legend>Cognito Sign Up User Demo</legend>
        User name: <input type="text" id="username" placeholder="Enter user name...">
        <br>
        <br>
        Password: <input type="text" id="password" placeholder="Enter password...">
        <br>
        <br>
        <div style="width:500px;">
            <button id="signupUser">Sign Up User</button>
        </div>
        <ul id="signupUserResults"></ul>
    </fieldset>
    <script>
        var attributeList = [];
        document.getElementById('signupUser').addEventListener('click', function () {
          userPool.signUp(document.getElementById('username').value, document.getElementById('password').value,
            attributeList, null,
            function (err, result) {
                if (err) {
                    alert(err);
                    return;
                }
                document.getElementById('signupUserResults').innerHTML = "Results: " + JSON.stringify(
                  result.user, null, 2);
                cognitoUser = result.user;
                console.log(cognitoUser);
            });
        });
    </script
</body>
</html>