AWS Cognito 自定义身份验证流程 - initiateAuth 给出错误

AWS Cognito custom authentication flow - initiateAuth giving error

我正在尝试使用 AWS Cognito 创建自定义身份验证流程,以便我可以通过电子邮件而不是通过 Cognito 触发器发送 MFA 代码。我正在使用 initiateAuth() 方法执行此操作,根据文档这是正确的;

https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_InitiateAuth.html https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/CognitoIdentityServiceProvider.html#initiateAuth-property

我的负载似乎是有效的,但是当我尝试用一​​个用户登录时,我收到错误 't.getauthparameters is not a function'

我浏览了其他一些 Whosebug 帖子,但没有任何帮助

知道出了什么问题吗?

这是我下面代码的一个片段:


const payload = {
          AuthFlow: 'CUSTOM_AUTH',
          ClientId: 'my client id', 
          AuthParameters: {
             USERNAME: $('input[name=username]').val(),
             PASSWORD: $('input[name=password]').val(),
             CHALLENGE_NAME: 'SRP_A'
          }
        };
        
        cognitoUser.initiateAuth(payload, {
            onSuccess: function(result) {
                // User authentication was successful
            },
            onFailure: function(err) {
                // User authentication was not successful
            },
            customChallenge: function(challengeParameters) {
                // User authentication depends on challenge response
                var verificationCode = prompt('Please input OTP code' ,'');
                cognitoUser.sendCustomChallengeAnswer(verificationCode, this);
            },
        });

所以我最终发现 initiateAuth() 不是正确的使用方法。

正确的使用方法是 cognitoUser.authenticateUser()(因为我使用基于 SRP 的身份验证然后添加自定义质询)-我的更新代码如下

This was a similar example that i followed to help me find the answer

我无法在网上找到很多仅使用 Amazon Cognito Identity SDK 来完成此操作的信息,因此希望这对任何正在做同样事情的人都有帮助!

AWSCognito.config.region = 'region';
        
        var poolData = {
            UserPoolId : 'user pool id', 
            ClientId : 'client id' 
        };
        var userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);
        
        var userData = {
            Username: $('input[name=username]').val(),
            Pool: userPool,
        };
        var authenticationData = {
            Username : $('input[name=username]').val(),
            Password : $('input[name=password]').val(),
        };

        var authenticationDetails = new AmazonCognitoIdentity.AuthenticationDetails(authenticationData);
        var cognitoUser = new AmazonCognitoIdentity.CognitoUser(userData);
        
        cognitoUser.setAuthenticationFlowType('CUSTOM_AUTH');
        
        cognitoUser.authenticateUser(authenticationDetails, {
            onSuccess: function(result) {
                console.log('success');
                var resultStr = 'Login Successful';
                console.log(resultStr);
                $('#resultsSignIn').html(resultStr);
            },
            onFailure: function(err) {
                alert(err);
            },
            customChallenge: function(challengeParameters) {
                // User authentication depends on challenge response
                var verificationCode = prompt('Please input OTP code' ,'');
                cognitoUser.sendCustomChallengeAnswer(verificationCode, this);
            },
        });
        
        return false;`