Aaroosh Pandoh
Aaroosh Pandoh

Reputation: 177

Forgot Password in cognito (if email is not verified)

I am trying forgot password flow with Cognito. But users who didn't verify the email aren't getting a verification code for forgot password. I have checked other StackOverflow posts as well but it is suggested to verify the user's email which will be the wrong way as it is not verified after all.

var userData = {
    Username: email,
    Pool: awsConfig.cognitoUserPool
};
var cognitoUser = new AmazonCognitoIdentity.CognitoUser(userData);    
    cognitoUser.forgotPassword({
            onSuccess: function (data) {
                // console.log(data);
                resolve(data);
            },
            onFailure: function (error) {
                reject(error);
            }
        });

Upvotes: 1

Views: 3596

Answers (1)

Tausif
Tausif

Reputation: 46

It's a late post, but I have a solution for you. Reset password would send a confirm code to the verified email. But if the email is not verified, it will be caught in the catch block of the async function. During this, send the verification link again to the email address provided (you can use Amplify's resendSignup -> https://aws-amplify.github.io/amplify-js/api/classes/authclass.html#resendsignup). This is because Cognito sends password confirm code ONLY to verified email addresses. Hope this helps you and other devs having similar issues. A code example is given below:

const handleSubmit = async () => {
setIsLoading(true)
const isError = handleInputValidation()
if (isError) {
  setIsLoading(false)
  return
}
try {
  await requestCode(input.email)
  setIsLoading(false)
  history.push({
    pathname: path.auth.resetPasswordAwait,
    state: input.email,
  })
} catch (error) {
  if (error.message.includes('verified')) {
    await resendSignUp(input.email)
    setErrorMessage(
      'Your email is not verified. A new verification link is sent to your email. Please confirm your email address.',
    )
  } else {
    setErrorMessage(error.message)
  }
  setIsLoading(false)
}

}

Upvotes: 3

Related Questions