简体   繁体   中英

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?

在此输入图像描述

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 amazon-cognito-identity-js@1.31.0 --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 aws-sdk@2.177.0 --save

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM