Amazon-cognito-identity-js: [authenticateUser] Uncaught TypeError: Cannot read property 'toBits' of undefined

Created on 23 Apr 2016  ·  12Comments  ·  Source: amazon-archives/amazon-cognito-identity-js

I was able to successfully sign up userPool.signUp and then successfully confirm cognitoUser.confirmRegistration.

But authentication of the user, so basically sign in with the following code fails

    // Cognito Identity Pool Id
    AWS.config.region = 'us-east-1'; // Region
    AWS.config.credentials = new AWS.CognitoIdentityCredentials({
        IdentityPoolId: 'us-east-1:XXX'
    });

    // Cognito User Pool Id
    AWSCognito.config.region = 'us-east-1';
    AWSCognito.config.credentials = new AWS.CognitoIdentityCredentials({
        IdentityPoolId: 'us-east-1:XXX'
    });

    var poolData = { UserPoolId : 'us-east-XXX',
        ClientId : 'XXX'
    };
    var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData);

    var authenticationData = {
        Username : 'testuser',
        Password : 'testpassword',
    };
    var authenticationDetails = new AWSCognito.CognitoIdentityServiceProvider.AuthenticationDetails(authenticationData);

    var userData = {
        Username : 'testuser',
        Pool : userPool
    };
    var cognitoUser = new AWSCognito.CognitoIdentityServiceProvider.CognitoUser(userData);
    cognitoUser.authenticateUser(authenticationDetails, {
        onSuccess: function (result) {
            console.log('access token + ' + result.getAccessToken().getJwtToken());
        },

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

    });

This is the error I get

aws-cognito-sdk.min.js:6 Uncaught TypeError: Cannot read property 'toBits' of undefined

Most helpful comment

correct i compiled the sjcl with code byte options and now it is working, uploading the working copy
sjcl.js.zip

All 12 comments

Hi Bernd,

In your case, I think the error happens because you haven't included the bytes codec in sjcl. By default, sjcl doesn't come with the bytes codec so you need to configure and make it with that particular option (this is mentioned in the README.md file).

Ionut.

correct i compiled the sjcl with code byte options and now it is working, uploading the working copy
sjcl.js.zip

Thanks @itrestian , that fixed it for me too.

Just wondering if all that will be merged into aws-sdk once it is out of beta?

I don't mind so much since I use webpack, but would still prefer everything in aws-sdk rather than across lots of libraries.

Also the AWS.config and AWSCognio.config feels a bit odd. Hopefully all that end up as a clean API within the aws-sdk.

Hi Bernd,

As a general rule, we don't comment on the future direction of products and features, but I will definitely mention this within the team as we are always interested in improving the user experience when using our products.

Ionut.

Alphatiger - the file you posted worked for me as well. Thanks!

@Alphatiger - The file you posted worked for me Excelent too. Thanks a lot.

@Alphatiger thanks 👍

I am using the same code mentioned in this issue. I am getting following error while authenticating a user

"amazon-cognito-identity.min.js:19Uncaught TypeError: Cannot read property 'hex' of undefined(…)"

I have used sjcl.js uploaded above but still getting this error. Please help

@amank12 note that include order can cause this issue, here is working includes files for AWS SDK https://maketips.net/tip/125/cannot-read-property-hex-of-undefined-in-aws-sdk

@ivictbor I'm in the same boat as @amank12. I included the script statement inside my index.html file before my cryptojs script tag and I added the file to my project but I'm still getting the same issue.

Maybe can you elaborate on the process of fixing this issue step by step? E.g. where in the project to include it and where I should include the script tag?

Thanks!

SJCL isn't used anymore.

https://github.com/aws/amazon-cognito-identity-js/pull/281

Latest version of this package uses internal AWS util methods to perform hash computations.

@owlstack here is zip file with very basic and simple demo using cognito login
cognito_login_demo.zip Change your identifiers in <script> section of index.html:

AWSCognito.config.region = 'us-west-2';
....
var poolData = {
      UserPoolId: 'us-west-2_SOMEID',
      ClientId: 'someClientId'
    };

And play with it. Than you can check how files included. And also it may be issue related to some versions of js files so try to use set from zip (of course better to use latest versions so if you want you may try to update file by file after you will have working configuration)
Hope this help

Was this page helpful?
0 / 5 - 0 ratings

Related issues

howardya picture howardya  ·  3Comments

tranan89 picture tranan89  ·  5Comments

sarah-pixvana picture sarah-pixvana  ·  4Comments

carlnordenfelt picture carlnordenfelt  ·  5Comments

sarah-pixvana picture sarah-pixvana  ·  5Comments