简体   繁体   English

AWS 节点 JS MFA Cognito

[英]AWS Node JS MFA Cognito

I was working on aws sdk for node js, and trying to authenticate an user from a particular user pool.我正在为 node js 开发 aws sdk,并尝试对来自特定用户池的用户进行身份验证。 NOTE: my user pool has Multi-factor authentication enabled and receives OTP via SMS.注意:我的用户池启用了多重身份验证并通过 SMS 接收 OTP。

This is my piece of code: ` var userData = { Username: 'username', Pool: userPool };这是我的一段代码:`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);
            }

        });` 

BUT: the problem is: It gives an error:但是:问题是:它给出了一个错误:

Error => {"code":"UnknownError", "message":"Unknown error, the response body from fetch is undefined"} Error => {"code":"UnknownError", "message":"Unknown error, fetch 的响应主体未定义"}

**And On Stack Trace I Got: ** 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) **在堆栈跟踪中我得到:** 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)

**BUT AGAIN:::: **The OTP comes to my mobile... **但又一次:::: **OTP 出现在我的手机上......

Please can anyone help me????请问谁能帮帮我????

Thanx in advance提前致谢

Add the missed callbacks functions so you can handle the state correctly:添加错过的回调函数,以便您可以正确处理状态:

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
}
global['fetch'] = require('node-fetch');

Use the above code at the top of your file.在文件顶部使用上面的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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