1

我正在为节点 js 开发 aws sdk,并尝试对来自特定用户池的用户进行身份验证。注意:我的用户池启用了多因素身份验证并通过 SMS 接收 OTP。

这是我的一段代码:` var userData = { Username : 'username', Pool : userPool };

        cognitoUser = new AmazonCognitoIdentity.CognitoUser(userData);

        var authenticationData = {
            Username : 'username',
            Password : 'password',
        };

        var authenticationDetails = new AmazonCognitoIdentity.AuthenticationDetails(authenticationData);

        cognitoUser.authenticateUser(authenticationDetails, {
            onSuccess: function (result) {
                console.log('authentication successful!')
            },

            onFailure: function(err) {
                alert(err);
            },

            mfaRequired: function(codeDeliveryDetails) {
                var verificationCode = prompt('Please input verification code' ,'');
                cognitoUser.sendMFACode(verificationCode, this);
            }

        });` 

但是:问题是:它给出了一个错误:

Error => {"code":"UnknownError", "message":"未知错误,来自 fetch 的响应正文未定义"}

**在堆栈跟踪上我得到了:**Stack Trace : Error at Object.onFailure (E:\Karma\node_aws\medium_try\index.js:78:79) at E:\Karma\node_aws\medium_try\node_modules\amazon-cognito-identity-js\lib\CognitoUser.js:376:31 at E:\Karma\node_aws\medium_try\node_modules\amazon-cognito-identity-js\lib\CognitoUser.js:361:22 at E:\Karma\node_aws\medium_try\node_modules\amazon-cognito-identity-js\lib\Client.js:114:14 at <anonymous> at process._tickDomainCallback (internal/process/next_tick.js:228:7)

**但又一次 :::: **OTP 来到我的手机...

请问谁能帮帮我???

提前感谢

4

2 回答 2

1

添加错过的回调函数,以便您可以正确处理状态:

export interface IAuthenticationCallback {
    onSuccess: (session: CognitoUserSession, userConfirmationNecessary?: boolean) => void,
    onFailure: (err: any) => void,
    newPasswordRequired?: (userAttributes: any, requiredAttributes: any) => void,
    mfaRequired?: (challengeName: any, challengeParameters: any) => void,
    totpRequired?: (challengeName: any, challengeParameters: any) => void,
    customChallenge?: (challengeParameters: any) => void,
    mfaSetup?: (challengeName: any, challengeParameters: any) => void,
    selectMFAType?: (challengeName: any, challengeParameters: any) => void
}
于 2018-12-26T10:53:15.273 回答
0
global['fetch'] = require('node-fetch');

在文件顶部使用上述代码。

于 2019-12-19T11:55:12.630 回答