Javascript 如何允许我的用户在 Cognito 用户池上重置密码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38110615/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 21:06:07  来源:igfitidea点击:

How to allow my user to reset their password on Cognito User Pools?

javascriptamazon-web-servicesamazon-cognito

提问by Mark Keane

So in my app I obviously want to provide the means for users to reset their passwords. The issue I'm having though is that the new documentation for User Pools is pretty ambiguous on this topic. Here is what they tell you to do for a Forgot Password flow, and the link you may find it at:

所以在我的应用程序中,我显然想为用户提供重置密码的方法。不过,我遇到的问题是用户池的新文档在这个主题上非常含糊不清。以下是他们告诉您为忘记密码流程执行的操作,您可以在以下链接找到它:

cognitoUser.forgotPassword({
        onSuccess: function (result) {
            console.log('call result: ' + result);
        },
        onFailure: function(err) {
            alert(err);
        },
        inputVerificationCode() {
            var verificationCode = prompt('Please input verification code ' ,'');
            var newPassword = prompt('Enter new password ' ,'');
            cognitoUser.confirmPassword(verificationCode, newPassword, this);
        }
    });

http://docs.aws.amazon.com/cognito/latest/developerguide/using-amazon-cognito-user-identity-pools-javascript-examples.html

http://docs.aws.amazon.com/cognito/latest/developerguide/using-amazon-cognito-user-identity-pools-javascript-examples.html

However when I drop this code into my project where a cognitoUser is defined and signed in, nothing seems to happen. I understand I need to somehow integrate this code with sending a verification code to the user, and asking them for a new password, but can't find anything on how to do this. Thoughts?

但是,当我将此代码放入定义并登录了 cognitoUser 的项目中时,似乎什么也没发生。我知道我需要以某种方式将此代码与向用户发送验证码并要求他们输入新密码相结合,但找不到有关如何执行此操作的任何信息。想法?

Thanks

谢谢

采纳答案by M Reddy

Resetting the password with forgot password flow has two steps:

使用忘记密码流程重置密码有两个步骤:

  1. Start the process by requesting for a verification code from the service. A code will be delivered to the user's phone/email.
  2. Set the new password using the delivered verification code.
  1. 通过从服务请求验证码开始该过程。代码将发送到用户的电话/电子邮件。
  2. 使用提供的验证码设置新密码。

Use these two functions to perform the above steps and reset the password:

使用这两个函数执行上述步骤并重置密码:

  1. cognitoUser.forgotPassword(): This will start the forgot password process flow. The service generates a verification code and sends it to the user. The "data", returned through callback.inputVerificationCode(data), indicates where the verification code was sent.

  2. cognitoUser.confirmPassword(): Use the delivered verification code with this function to set a new password.

  1. cognitoUser.forgotPassword():这将启动忘记密码的流程。该服务生成验证码并将其发送给用户。通过 callback.inputVerificationCode(data) 返回的“数据”表示验证码发送到哪里。

  2. cognitoUser.confirmPassword():使用本功能附带的验证码设置新密码。

回答by user1322092

AWS' docs are terrible on this topic (Cognito). You basically need to setup cognitoUser, then call forgotPassword

AWS 的文档在这个主题上很糟糕(Cognito)。您基本上需要设置cognitoUser,然后调用forgotPassword

export function resetPassword(username) {
    // const poolData = { UserPoolId: xxxx, ClientId: xxxx };
    // userPool is const userPool = new AWSCognito.CognitoUserPool(poolData);

    // setup cognitoUser first
    cognitoUser = new AWSCognito.CognitoUser({
        Username: username,
        Pool: userPool
    });

    // call forgotPassword on cognitoUser
    cognitoUser.forgotPassword({
        onSuccess: function(result) {
            console.log('call result: ' + result);
        },
        onFailure: function(err) {
            alert(err);
        },
        inputVerificationCode() { // this is optional, and likely won't be implemented as in AWS's example (i.e, prompt to get info)
            var verificationCode = prompt('Please input verification code ', '');
            var newPassword = prompt('Enter new password ', '');
            cognitoUser.confirmPassword(verificationCode, newPassword, this);
        }
    });
}

// confirmPassword can be separately built out as follows...  
export function confirmPassword(username, verificationCode, newPassword) {
    cognitoUser = new AWSCognito.CognitoUser({
        Username: username,
        Pool: userPool
    });

    return new Promise((resolve, reject) => {
        cognitoUser.confirmPassword(verificationCode, newPassword, {
            onFailure(err) {
                reject(err);
            },
            onSuccess() {
                resolve();
            },
        });
    });
}

回答by gabriel

I had this same issue. Was able to work through it by using confirmPassword() in the following way.

我有同样的问题。能够通过以下方式使用 confirmPassword() 来解决它。

//validation of input from form
req.checkBody('email', 'Username is required').notEmpty();
req.checkBody('password', 'Password is required').notEmpty();
req.checkBody('confirmationcode', 'Confirmation Code is required').notEmpty();


var confirmationCode = req.body.confirmationcode;
var password = req.body.password;
var userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);


var userData = {
    Username: req.body.email,
    Pool: userPool
};
var cognitoUser = new AmazonCognitoIdentity.CognitoUser(userData);

cognitoUser.confirmPassword(confirmationCode, password, {
    onFailure(err) {
        console.log(err);
    },
    onSuccess() {
        console.log("Success");
    },
});