AWS Cognito Admin创建了用户临时密码验证&重启

时间:2018-03-14 23:12:41

标签: javascript amazon-web-services aws-lambda aws-sdk amazon-cognito

我正在尝试验证管理员通过密码重置挑战创建用户使用AWS Cognito生成的临时密码,我找不到如何使用临时密码和设置新密码的方法或示例对于javascript中的新用户。

2 个答案:

答案 0 :(得分:1)

Amazon Cognito开发人员指南提供了使用临时密码进行身份验证并处理newPasswordRequired条件的示例:

cognitoUser.authenticateUser(authenticationDetails, {
    onSuccess: [...],
    onFailure: [...],
    mfaRequired: [...],
    newPasswordRequired: function(userAttributes, requiredAttributes) {
        // User was signed up by an admin and must provide new 
        // password and required attributes, if any, to complete 
        // authentication.

        // userAttributes: object, which is the user's current profile. It will list all attributes that are associated with the user. 
        // Required attributes according to schema, which don’t have any values yet, will have blank values.
        // requiredAttributes: list of attributes that must be set by the user along with new password to complete the sign-in.


        // Get these details and call 
        // newPassword: password that user has given
        // attributesData: object with key as attribute name and value that the user has given.
        cognitoUser.completeNewPasswordChallenge(newPassword, attributesData, this)
    }
});

摘自指南:https://docs.aws.amazon.com/cognito/latest/developerguide/using-amazon-cognito-identity-user-pools-javascript-example-authenticating-admin-created-user.html

请注意,示例中completeNewPasswordChallenge的第三个参数是this,即具有处理函数的对象。这是因为completeNewPasswordChallenge需要onSuccessonFailure处理程序,并且您通常可以使用与authenticateUser结果相同的处理程序。

答案 1 :(得分:0)

我确实浏览了你提到的文件。我不明白应该是什么'attributesData'。以下是我到目前为止所做的事情。

var authenticationData = {
       Username : email,
       Password : temppassword,
   };
   var authenticationDetails = new AWSCognito.CognitoIdentityServiceProvider.AuthenticationDetails(authenticationData);
   cognitoUser.authenticateUser(authenticationDetails, {
       onSuccess: function (result) {
           console.log('access token + ' + result.getAccessToken().getJwtToken());
           console.log('idToken + ' + result.idToken.jwtToken);// User authentication was successful
       },

       onFailure: function(err) {
           alert(err);// User authentication was not successful
       },

       newPasswordRequired: function(userAttributes, requiredAttributes) {
           userAttributes: authenticationData; 
           requiredAttributes: email;
           var newPassword: password;
           // attributesData: object with key as attribute name and value that the user has given.
           cognitoUser.completeNewPasswordChallenge(newPassword, attributesData, this)
       }
   });