Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 906
Suhail Gupta Avatar asked Jan 09 '18 18:01

Suhail Gupta


1 Answers

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

like image 67
nrg Avatar answered Oct 28 '22 02:10

nrg