首页 > 解决方案 > AWS Cognito Node.JS 用户身份验证返回未知问题

问题描述

我将 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.jsamazon-web-servicesamazon-cognito

解决方案


在 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');

推荐阅读