AWS cognito forgot password flow

Viewed 4211

I've created an AWS cognito user pool with email as required attribute and checked email for verification. The users are created from my java spring backend service using AWSCognitoClient sdk and calling adminCreateUser(createUser) method. The user gets an email with temporary password, which on signing in for the first time will set new password. Now when I execute the forgot password flow, I get the following error,

 InvalidParameterException: Cannot reset password for the user as there is no registered/verified email or phone_number

Although I have received a temporary password to the email id I signed up for and changed my password for very first time I get the above error. Can someone explain what am I missing?

Below is the javascript code am executing for forgot password flow,

forgotPassword(username: String, poolInfo:any){

       var poolData = {
            UserPoolId : poolInfo.poolId, // Your user pool id here
            ClientId : poolInfo.portalClientId // Your client id here
        };

        var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData);

        var userData = {
            Username : username,
            Pool : userPool
        };

        var cognitoUser = new AWSCognito.CognitoIdentityServiceProvider.CognitoUser(userData);

        cognitoUser.forgotPassword({
            onSuccess: function (result) {

            this.router.navigate(['login']);

            },
            onFailure: function(err) {
                alert(err);
            },
            //Optional automatic callback
            inputVerificationCode: function(data) {
                var verificationCode = prompt('Please input verification code ' ,'');
                var newPassword = prompt('Enter new password ' ,'');
                cognitoUser.confirmPassword(verificationCode, newPassword, this);
            }
        });
    }
2 Answers

I solved this issue with python:

response = cognito_client.get_user_attribute_verification_code(AccessToken='eyJraWQiOiJtTEM4Vm......',AttributeName='email')

response = cognito_client.verify_user_attribute( AccessToken='eyJraWQiOiJtTEM......', AttributeName='email', Code='230433')

def forgot_password(usename):
    ClientId = 'f2va............'

    response = cognito_client.forgot_password( ClientId=ClientId, Username=username)
def confirm_forgot_password():
    ClientId = 'f2va............'
    response = cognito_client.confirm_forgot_password(ClientId=ClientId,Username=username,ConfirmationCode='644603',Password='12345678')

Here is the Official Documentation. https://boto3.amazonaws.com/v1/documentation/api/1.9.42/reference/services/cognito-idp.html#CognitoIdentityProvider.Client.confirm_forgot_password

Related