简体   繁体   English

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

[英]AWS Cognito custom authentication flow - initiateAuth giving error

I am trying to make a custom authentication flow using AWS Cognito so that i can send MFA codes via email instead through the cognito triggers.我正在尝试使用 AWS Cognito 进行自定义身份验证流程,以便我可以通过 email 而不是通过 cognito 触发器发送 MFA 代码。 I am using the initiateAuth() method to do this which is correct according to the documentation;我正在使用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-property 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 -财产

My payload seems to be valid but when i try login with a user i get the error 't.getauthparameters is not a function'我的有效负载似乎是有效的,但是当我尝试使用用户登录时,我收到错误“t.getauthparameters 不是函数”

I've had a look through some other stackoverflow posts but nothing is helping我浏览了其他一些stackoverflow帖子,但没有任何帮助

Any ideas what is going wrong?任何想法出了什么问题?

This is a snippet from my code below:这是我下面代码的一个片段:


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

So i ended up finding out that initiateAuth() is not the correct method to use.所以我最终发现initialAuth()不是正确的使用方法。

The right method to use is cognitoUser.authenticateUser() (since i am using SRP-based authentication then adding a custom challenge) - My updated code is below使用正确的方法是 cognitoUser.authenticateUser() (因为我使用基于 SRP 的身份验证然后添加自定义挑战) -我的更新代码如下

This was a similar example that i followed to help me find the answer 这是一个类似的例子,我遵循它来帮助我找到答案

I couldnt find very much online for doing it with just the Amazon Cognito Identity SDK so hopefully this is helpful for anyone doing the same!仅使用 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;`

A downside to the authenticateUser() method is that you won't be able to get user's input mid-execution during the authenticateUser workflow (ie, having to use prompts in the callbacks for customchallenge etc). authenticateUser() 方法的一个缺点是您将无法在 authenticateUser 工作流程的执行过程中获得用户的输入(即,必须在回调中使用提示来进行 customchallenge 等)。 I believe initiateAuth() would solve this issue.我相信initialAuth() 会解决这个问题。

https://docs.aws.amazon.com/cognito/latest/developerguide/user-pool-lambda-define-auth-challenge.html https://docs.aws.amazon.com/cognito/latest/developerguide/user-pool-lambda-define-auth-challenge.html

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

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