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

后端 未结 3 1889
难免孤独
难免孤独 2021-02-05 03:47

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 ambigu

3条回答
  •  -上瘾入骨i
    2021-02-05 04:37

    AWS' docs are terrible on this topic (Cognito). You basically need to setup cognitoUser, then call 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();
                },
            });
        });
    }
    

提交回复
热议问题