0
votes

So I used this chunk of code found on the official repo of AWS JS SDK. It is used to authenticate a user.

It is returning a blank response.

AWSCognito.config.region = 'us-east-1';
var authenticationData = {
    Username : '+1112223333', //some phone number used as an Alias
    Password : 'password123456',
};
var authenticationDetails = new AWSCognito.CognitoIdentityServiceProvider.AuthenticationDetails(authenticationData);
var poolData = {
    UserPoolId : 'us-east-1_P00l1d', // Your user pool id here
    ClientId : 'xxx' // Your client id here
};
var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData);
var userData = {
    Username : 'myusername',
    Pool : userPool
};
var cognitoUser = new AWSCognito.CognitoIdentityServiceProvider.CognitoUser(userData);
cognitoUser.authenticateUser(authenticationDetails, {
    onSuccess: function (result) {
        console.log('access token + ' + result.getAccessToken().getJwtToken());
        AWS.config.credentials = new AWS.CognitoIdentityCredentials({
            IdentityPoolId : 'xxx', // your identity pool id here
            Logins : {
                // Change the key below according to the specific region your user pool is in.
                'cognito-idp.pool_id_number_here_xxx' : result.getIdToken().getJwtToken()
            }
        });
    },

    onFailure: function(err) {
        alert(err)
        console.log("Error " + err);
    },

});

So, for the authenticationData, I used the phone number as username (phone number is set as an alias) and the password. I tried with the username directly as well. The UserPoolId and ClientId are correct as I used the same value for registering and confirming the phone number. In the userData, I set the Username with the proper myusername.

About the Identity Pool, I created an Identity Pool on AWS Console linked to my App and my UserPool and I replaced the values IdentityPoolId and Logins in authenticateUser. I am not completely sure about the value in Logins though. I used cognito-idp.POOLIDNUMBER.

The output from AWS is blank. I am thinking that I can not even reach the server and I suspect an issue with the roles or the Identity Pool (the userPool is fine I suppose).

My identity pool is only using AWS Cognito users, not Facebook or other Identity Providers.

2

2 Answers

0
votes

Recompiling SJCL with --with-all solved the issue.

0
votes

Please make sure you have all the necessary libraries for 'Amazon Cognito Identity SDK for JavaScript'. Following is the list and links to these libraries.

1.The Amazon Cognito AWS SDK for JavaScript - https://raw.githubusercontent.com/aws/amazon-cognito-identity-js/master/dist/aws-cognito-sdk.min.js

2.The Amazon Cognito Identity SDK for JavaScript - https://raw.githubusercontent.com/aws/amazon-cognito-identity-js/master/dist/amazon-cognito-identity.min.js

3.JavaScript BN library (jsbn.js,jsbn2.js) - http://www-cs-students.stanford.edu/~tjw/jsbn/

4.Stanford JavaScript Crypto Library - https://github.com/bitwiseshiftleft/sjcl

5.AWS SDK for JavaScript (optional, to use other aws services) - http://aws.amazon.com/sdk-for-browser/

Include all these libraries and then use the code snippet below.

AWSCognito.config.region = 'YOUR_REGION';
var poolData = { 
    UserPoolId : 'YOUR_USER_POOL_ID', // Your user pool id here
    ClientId : 'YOUR_CLIENT_ID' // Your client id here
};
var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData);
var userData = {
    Username : "userName", // your username here
    Pool : userPool
};
//Signing Users in the App
var authenticationData = {
    Username : "userName", // your username here
    Password : "password" // your password here
};

var authenticationDetails = new AWSCognito.CognitoIdentityServiceProvider.AuthenticationDetails(authenticationData);

var cognitoUser = new AWSCognito.CognitoIdentityServiceProvider.CognitoUser(userData);

cognitoUser.authenticateUser(authenticationDetails, {
    onSuccess: function (result) {
        console.log('access token + ' + result.getAccessToken().getJwtToken());
    },
    onFailure: function(err) {
        console.log(err);
    }
});