【问题标题】:AWS Cognito Node.JS User Authentication returns unknown problemAWS Cognito Node.JS 用户身份验证返回未知问题
【发布时间】:2019-04-02 15:35:06
【问题描述】:

我将 AWS Cognito 与 Node.JS 结合使用。

我已成功注册和验证用户,但身份验证返回“未知错误,来自 fetch 的响应正文未定义。”

我正在使用 node-fetch 模块和 amazon-cognito-identity-js(在下面的代码中设置为 var AWSCognito)。用户未处于需要更改密码和验证的状态。

其他人是否遇到过这种情况,您是如何解决这个问题的? 提前感谢任何指导....

这是我的代码,我的完整模块在 npm 上作为 iditawsutils :

exports.authCognitoUser = function(theUserPoolID, theClientID, userName, userPassword) {

    var authenticationData = {
        Username : userName,
        Password : userPassword
    };

    var authenticationDetails = new AWSCognito.AuthenticationDetails(authenticationData);

    var poolData = { UserPoolId : theUserPoolID,
        ClientId : theClientID
    };
    var userPool = new AWSCognito.CognitoUserPool(poolData);

    var userData = {
        Username : userName,
        Pool : userPool
    };

    console.log('authentication details: ',authenticationDetails);

    var cognitoUser = new AWSCognito.CognitoUser(userData);

    cognitoUser.authenticateUser(authenticationDetails, {
        onSuccess: function (result) {
            console.log('access token + ' + result.getAccessToken().getJwtToken());
            console.log('id token + ' + result.getIdToken().getJwtToken());
            console.log('refresh token + ' + result.getRefreshToken().getToken());
            return result;
        },
        onFailure: function(err) {
            console.log(err.message || JSON.stringify(err));
            return err;
        },

    });

}

//来自控制台日志:

authentication details:  AuthenticationDetails {
  validationData: {},
  authParameters: {},
  username: 'thesmarterstuff',
  password: 'passW0rd!’ }

未知错误,来自 fetch 的响应正文是:未定义

【问题讨论】:

    标签: node.js amazon-web-services amazon-cognito


    【解决方案1】:

    在 onFailure 块中使用以下内容来查找有关错误的更多详细信息。

    onFailure: function(err) {
            console.log(new Error().stack);
            console.log(err.message || JSON.stringify(err));
        },
    

    如果您在 Client.js 中的 fetch 行中发现错误,那么这可能是因为当前 NodeJS SDK 和大多数其他 SDK 不支持默认的 USER_SRP_AUTH。

    您可以通过在 Client.js 中添加 console.log 来检查

    console.log(this.endpoint);
    console.log(options);
    

    登录到您的 AWS 账户,并确保您已选中选项 - 启用基于应用程序的身份验证的用户名-密码(非 SRP)流 (USER_PASSWORD_AUTH)

    然后,在您的代码中使用以下设置更新它。

    cognitoUser.setAuthenticationFlowType('USER_PASSWORD_AUTH');
    

    【讨论】:

    • 感谢您抽出宝贵时间帮助我!我很感激。我设置了选项,但没有 cognitoUser.setAuthenticationFlowType('USER_PASSWORD_AUTH');我的代码中的行...现在获取访问令牌和刷新令牌。谢谢!史蒂夫
    猜你喜欢
    • 2017-01-02
    • 2022-06-19
    • 1970-01-01
    • 2018-01-29
    • 2016-01-17
    • 2017-10-08
    • 2016-06-25
    • 1970-01-01
    • 2018-03-29
    相关资源
    最近更新 更多