util.crypto.lib。 randomBytes不是函数:aws cognito js会在身份验证时抛出错误

时间:2018-01-09 18:06:35

标签: javascript reactjs amazon-web-services amazon-cognito

我收到以下错误:

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

当我尝试使用以下代码对用户进行身份验证时,我写道:

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);
    }
});

这可能是什么原因?我错过了什么?

enter image description here

2 个答案:

答案 0 :(得分:6)

更新(2018年1月12日):

amazon-cognito-identity-js开发人员已将aws-sdk版本锁定在v1.31.0中,因此您不必再降级aws-sdk,只需升级该软件包:

npm install amazon-cognito-identity-js@1.31.0 --save

看起来aws-sdk包存在问题。它是您正在使用的amazon-cognito-identity-js包的依赖关系。

您可以尝试通过运行降级来降级:

npm install aws-sdk@2.177.0 --save

答案 1 :(得分:0)

降级是目前唯一的选择。我几个小时都遇到了这个错误,无法弄清楚实际问题。我想AWS应该保持适当的版本控制并且只推送稳定版本。 :\ aws-sdk 2.177.0工作正常。您可以使用AWS的angular-2-cognito样板。