util.crypto.lib. randomBytes is not a function : aws cognito js throws error on authentication

I get the following error:

TypeError: __WEBPACK_IMPORTED_MODULE_0_aws_sdk_global__.util.crypto.lib.
randomBytes is not a function

when I try to authenticate the user using the following code I wrote:

import { CognitoUserPool, CognitoUserAttribute, CognitoUser, AuthenticationDetails } 
from 'amazon-cognito-identity-js';

let authenticationDetails = new AuthenticationDetails({
    Username: username,
    Password: password
});

let userPool = new CognitoUserPool({
    UserPoolId: 'us-east-1_1TXXXXXXbXX',
    ClientId: '4da8hrXXXXXXXXXXXXmj1'
});

let cognitoUser = new CognitoUser({
    Username: username,
    Pool: userPool
});

// THE ERROR IS THROWN AS SOON AS IT HITS THE BELOW
// STATEMENT
cognitoUser.authenticateUser(authenticationDetails, {
    onSuccess: function (result) {
        console.log('access token + ' + result.getAccessToken().getJwtToken());
    },
    onFailure: function(err) {
        console.log(err);
    }
});

What could be the reason for this? What am I missing?

enter image description here

Answers:

Answer

Update (12 Jan 2018):

The amazon-cognito-identity-js devs have locked the aws-sdk version in v1.31.0, so you don't have to downgrade aws-sdk anymore, just upgrade the package:

npm install [email protected] --save

Looks like there's a problem with the aws-sdk package. It's the dependency of the amazon-cognito-identity-js package that you're using.

You could try to downgrade it by running:

npm install [email protected] --save

Answer

Downgrading is the only option right now. I faced this error for hours, unable to figure out the actual issue. I guess AWS should maintain proper versioning and push only stable versions. :\ aws-sdk 2.177.0 works just fine. You can use the angular-2-cognito boilerplate by AWS.

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.