Ankit
Ankit

Reputation: 67

Uncaught ReferenceError: AWSCognito Is Not Defined

I am trying to connect AWS Cognito using browser-script approach. Getting Uncaught ReferenceError: AWSCognito is not defined using the below code from AWS Blog https://aws.amazon.com/blogs/mobile/accessing-your-user-pools-using-the-amazon-cognito-identity-sdk-for-javascript/. Snippet of the attached error.

enter image description here

**

<!DOCTYPE html>

<html lang="en">
  <head>
    <meta charset="utf-8">
    <!-- Javascript SDK-->
 
  <script src="https://sdk.amazonaws.com/js/aws-sdk-2.410.0.min.js"></script> 

 
  </head>

    
  <body>

    <h1 class="h3 mb-3 font-weight-normal" id="titleheader">Register an Account</h1>
    
    <input type="personalname" class="form-control" id="personalnameRegister" placeholder="Name" pattern=".*" required>
    <input type="email" class="form-control" id="emailInputRegister" placeholder="Email" pattern=".*" required>
    <input type="password" class="form-control" id="passwordInputRegister" placeholder="Password" pattern=".*" required>
    <!-- <input type="password" class="form-control" id="confirmationpassword" placeholder="Confirm Password" pattern=".*" required> -->
    <button id="mainbutton" class="btn btn-lg btn-primary btn-block" type="button" onclick="registerButton()" >Register</button>

<script type="text/javascript">
// Amazon Cognito creates a session which includes the id, access, and refresh tokens of an authenticated user.
function registerButton() {

username = document.getElementById("emailInputRegister").value;
password =  document.getElementById("passwordInputRegister").value; 

var authenticationData = {
        Username : username,
<!--         Password : password, -->
    };
 var authenticationDetails = new AWSCognito.CognitoIdentityServiceProvider.AuthenticationDetails(authenticationData);
    var poolData = { UserPoolId :<POOLID>,
        ClientId :<clientId>
    };
    var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData);
    <!-- var userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData); -->
    Console.log(userPool)
    var userData = {
        Username : document.getElementById("emailInputRegister").value,
        Pool : userPool
    };
    
     var cognitoUser = new AWSCognito.AmazonCognitoIdentity.CognitoUser(userData);
    cognitoUser.authenticateUser(authenticationDetails, {
        onSuccess: function (result) {
            var accessToken = result.getAccessToken().getJwtToken();

            /* Use the idToken for Logins Map when Federating User Pools with identity pools or when passing through an Authorization Header to an API Gateway Authorizer */
            var idToken = result.idToken.jwtToken;
        },

        onFailure: function(err) {
            alert(err);
        },

}); 
}   
</script> 
</body> 
</html>

**

Upvotes: 0

Views: 1118

Answers (1)

user2263572
user2263572

Reputation: 5606

You need to include the amazon-cognito js files in your head element. You can check your current console to see that AWSCognito is currently undefined.

<script src="https://rawgit.com/aws/amazon-cognito-identity-js/master/dist/aws-cognito-sdk.min.js"></script>
<script src="https://rawgit.com/aws/amazon-cognito-identity-js/master/dist/amazon-cognito-identity.min.js"></script>

Upvotes: 1

Related Questions