amazon-archives / amazon-cognito-identity-js

Amazon Cognito Identity SDK for JavaScript
Other
986 stars 452 forks source link

ReferenceError: AWSCognito is not defined #632

Closed ellier closed 6 years ago

ellier commented 6 years ago

I'm getting this error while trying the Use Case #4:

    $ node util.js 
    /Users/myuser/Desktop/ruckusauth/util.js:16
    var authenticationDetails = new AWSCognito.CognitoIdentityServiceProvider.AuthenticationDetails(authenticationData);
                                    ^
    ReferenceError: AWSCognito is not defined
        at Object.<anonymous> (/Users/myuser/Desktop/cognitotest/util.js:16:33)
        at Module._compile (module.js:570:32)
        at Object.Module._extensions..js (module.js:579:10)
        at Module.load (module.js:487:32)
        at tryModuleLoad (module.js:446:12)
        at Function.Module._load (module.js:438:3)
        at Module.runMain (module.js:604:10)
        at run (bootstrap_node.js:394:7)
        at startup (bootstrap_node.js:149:9)
        at bootstrap_node.js:509:3

Here is the code, which is basically what I copied and pasted from here:

    const AWS = require("aws-sdk");
    var AmazonCognitoIdentity = require("amazon-cognito-identity-js");
    var CognitoUserPool = AmazonCognitoIdentity.CognitoUserPool;

    var username = 'john@doe.net';
    var password = 'qwerty';
    var poolid = 'us-east-1_ahkjhlk';
    var clientid = 'sdfasdfasdfwerter';
    var region = 'us-east-1';

    var authenticationData = {
        Username : username,
        Password : password,
    };
    var authenticationDetails = new AWSCognito.CognitoIdentityServiceProvider.AuthenticationDetails(authenticationData);
    var poolData = {
        UserPoolId : poolid, // Your user pool id here
        ClientId : clientid // Your client id here
    };
    var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData);
    var userData = {
        Username : username,
        Pool : userPool
    };
    var cognitoUser = new AWSCognito.CognitoIdentityServiceProvider.CognitoUser(userData);
    cognitoUser.authenticateUser(authenticationDetails, {
        onSuccess: function (result) {
            console.log('access token + ' + result.getAccessToken().getJwtToken());

            AWS.config.region = region;

            var loginstring = 'cognito-idp.' + region + '.amazonaws.com/' + poolid;
            AWS.config.credentials = new AWS.CognitoIdentityCredentials({
                IdentityPoolId : poolid, // your identity pool id here
                Logins : {
                    loginstring : result.getIdToken().getJwtToken()
                }
            });

            AWS.config.credentials.refresh((error) => {
                if (error) {
                     console.error(error);
                } else {
                     console.log('Successfully logged!');
                }
            });
        },

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

I'd really appreciate any help here. Thanks.

itrestian commented 6 years ago

I think the error is that you haven't defined AWSCognito. The following works for me in node:

var cognito = require('amazon-cognito-identity-js');

var poolData = { UserPoolId : 'YOUR_USER_POOL_ID',
    ClientId : 'YOUR_CLIENT_ID'
};

var userPool = new cognito.CognitoUserPool(poolData);

var authenticationData = {
   Username : 'YOUR_USERNAME',
   Password : 'YOUR_PASSWORD',
};

var authenticationDetails = new cognito.AuthenticationDetails(authenticationData);
var userData = {
    Username : 'YOUR_USERNAME',
    Pool : userPool
};

var cognitoUser = new cognito.CognitoUser(userData);
cognitoUser.authenticateUser(authenticationDetails, {
    onSuccess: function (result) {
        console.log('access token + ' + result.getAccessToken().getJwtToken());
    },

    onFailure: function(err) {
        console.log('error ' + err);
    },
});
ellier commented 6 years ago

Thanks for your reply. That worked.

I'm still wondering why I'm getting this AWSCognito undefined error when I'm just following the examples here.

itrestian commented 6 years ago

You're following the examples for script usage. If you look, there's a disclaimer saying: "The usage examples below use the unqualified names for types in the Amazon Cognito Identity SDK for JavaScript. Remember to import or qualify access to any of these types:"

ellier commented 6 years ago

I did all that and I'm still having the same issue. What am I missing?

itrestian commented 6 years ago

I'm not entirely sure I understand the issue at this point. My node example is working for you and I've explained that it's only in script usage that we namespace everything to AWSCognito. For node usage (as mentioned in the docs), you would have to require the objects from the corresponding modules, and you just haven't defined AWSCognito anywhere.