1

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

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 -财产

我的有效负载似乎是有效的,但是当我尝试使用用户登录时,我收到错误“t.getauthparameters 不是函数”

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

任何想法出了什么问题?

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


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);
            },
        });
4

1 回答 1

1

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

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

这是一个类似的例子,我遵循它来帮助我找到答案

仅使用 Amazon Cognito 身份开发工具包我无法在网上找到太多信息,所以希望这对任何做同样事情的人都有帮助!

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;`
于 2022-01-06T23:06:21.167 回答