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

时间:2016-06-29 21:48:01

标签: javascript amazon-web-services amazon-cognito

所以在我的应用程序中,我显然希望为用户提供重置密码的方法。我遇到的问题是用户池的新文档在这个主题上非常模糊。以下是他们告诉您的Forgot Password流程以及您可以在以下网址找到的链接:

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

然而,当我将此代码放入我的项目中,其中定义并登录了cognitoUser时,似乎没有任何事情发生。我知道我需要以某种方式集成此代码并向用户发送验证码,并要求他们提供新密码,但无法找到有关如何执行此操作的任何内容。想法?

由于

4 个答案:

答案 0 :(得分:38)

AWS' docs在这个主题上很糟糕(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();
            },
        });
    });
}

答案 1 :(得分:10)

使用忘记密码流重置密码有两个步骤: 1)通过从服务请求验证码来启动该过程。代码将发送到用户的电话/电子邮件。 2)使用提供的验证码设置新密码。

使用这两个功能执行上述步骤并重置密码: 1)cognitoUser.forgotPassword():这将启动忘记密码处理流程。该服务生成验证码并将其发送给用户。通过callback.inputVerificationCode(data)返回的“data”表示验证代码的发送位置。 2)cognitoUser.confirmPassword():使用此功能提供的验证码设置新密码。

答案 2 :(得分:5)

我有同样的问题。能够通过以下方式使用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");
    },
});

答案 3 :(得分:0)

如果像我一样,你发现如何用放大来处理这种情况

import { Auth } from 'aws-amplify';

// Send confirmation code to user's email
Auth.forgotPassword(username)
    .then(data => console.log(data))
    .catch(err => console.log(err));

// Collect confirmation code and new password, then
Auth.forgotPasswordSubmit(username, code, new_password)
    .then(data => console.log(data))
    .catch(err => console.log(err));

https://docs.amplify.aws/lib/auth/manageusers/q/platform/js#forgot-password