如何让应用程序用户通过AWS-cognito使用其当前密码更改密码?

时间:2019-02-25 09:30:22

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

我需要让用户通过aws-cognito随意更改密码,而无需进行“忘记密码”重置。应该使用他们的旧密码。

This the JSFiddle on how I am trying to do it in cognito

可以使用“忘记密码”重设密码,并且可以正常工作。但是我找不到来自Amazon Web Services本身的任何文档。请帮忙!

<div id="changePassForm" class="modal fade" aria-hidden="true">
<div class="modal-dialog">
    <div class="modal-content">
        <div class="modal-body">
            <div class="ibox-content">
                <form id="changePasswordForm" method="post" role="form">
                    <div class="row" id="pwd-container1">
                        <div class="col-sm-12">
                            <h2 class="text-center"><strong>Change Password</strong></h2>
                            <div class="form-group"><input id="currentPassword" type="password" placeholder="Old Password" class="form-control" required></div>
                            <div class="form-group"><input id="newPassword" type="password" placeholder="New Password" class="form-control password1" pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}" required></div>
                            <div class="form-group"><div class="pwstrength_viewport_progress"></div></div>
                            <div class="form-group"><input id="confirmPassword" type="password" placeholder="Confirm Password" class="form-control" required></div>
                            <div class="form-group"><button type="submit" class="btn btn-primary block full-width m-b"><strong>Change</strong></button></div>
                            <div class="form-group"><p>New Password Must Include:<br>- At least 8 or more characters,<br>- At least one number,<br>- At least one uppercase letter, and,<br>- At least one lowercase letter</p></div>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

function changePassword(email, oldPassword, password, onSuccess, onFailure) { createCognitoUser(email).changePassword(oldPassword, password, function changePasswordCallback(err, result) { if (!err) { console.log('call result: ' + result); onSuccess(result); } else { onFailure(err); } } ); }

$(function onDocReady() {
    $('#changePasswordForm').submit(handleChangePassword);
});

    function handleChangePassword(event) {
    var oldPassword = $('#currentPassword').val();
    var password = $('#newPassword').val();
    var password2 = $('#confirmPassword').val();
    var email = sessionStorage.getItem("changePassEmail");

    var onSuccess = function changePasswordSuccess(result) {
        var cognitoUser = result.user;
        console.log('user name is ' + cognitoUser.getUsername());
        var confirmation = ('Password change successful');
        if (confirmation) {
            alert('Password successfully changed');
        }
    };
    var onFailure = function changePasswordFailure(err) {
        alert(err);
    };
    event.preventDefault();

    if (password === password2) {
        changePassword(email, oldPassword, password, onSuccess, onFailure);
    } else {
        alert('Passwords do not match');
    }
}

0 个答案:

没有答案