skip to Main Content

I am using Amplify and creating a forgotten password journey for my app. I am using this function:

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));

As specified in the docs here

What I want to achieve is a 3 page journey.

  1. enter email address
  2. enter code (and have it validated before moving on to the next page)
  3. enter new password

The issue that I am having is that the provided forgotPasswordSubmit function takes all 3 args, when at this point, I only want to send 2 (email and code).

My plan was to send the email, and the code, and an empty string for the password, and then check the returned errors to see if there was one relating to the code being incorrect however I only ever get an error for the password being invalid.

Is there a way that I can get amplify to send back all errors at once (in this case the password being invalid but ALSO the code not being correct), or is there a nicer way of doing this?

I have searched for ages for an answer before asking here so any help would be much appreciated.

3

Answers


  1. I believe you can only do it as a two stage process.

    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));
    
    Login or Signup to reply.
  2. I have exactly the same problem. In our website, the forgotPassword flow looks like this:

    request a verification code -> verify code -> input new password

    But with the AWS amplify methods, I can ONLY do the step 2nd and 3rd TOGETHER.

    Hope if there is a way to solve this problem.

    Login or Signup to reply.
  3. I figured about a way to get around this problem. It’s definitely not ideal, but it works!

    1. Try forgotPasswordSubmit with a short invalid password.
    2. Check if the error message is about invalid code
    submitVerificationCode() {
      try {
      
        await Auth.forgotPasswordSubmit(
          username,
          confirmationCodes.join(""),
          "1111"
        );
      } catch (error) {
        if (
          error.message ===
          "Invalid verification code provided, please try again."
        ) {
          // Ask valid code again
        } else {
          // If the error message is not Invalid verification code error
          // then move to the next step: get a new password from the user
        }
      }
    }
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search