Signup with AWS Cognito

Hi, I have made this work:

async function handleSubmit(event) {
    event.preventDefault();
    setIsLoading(true);
    try {
      const newUser = await Auth.signUp({
        username: fields.email,
        password: fields.password,
      });
      setIsLoading(false);
      setNewUser(newUser);
    } catch (e) {
      if (e.name === "UsernameExistsException") {
        handleUsernameExistsException();
      }
      onError(e);
      setIsLoading(false);
    }
  }

  async function handleUsernameExistsException() {
    try {
      await Auth.signIn(fields.email, fields.password);
      userHasAuthenticated(true);
      nav("/");
    } catch (e) {
      if (e.name === "UserNotConfirmedException") {
        handleUserNotConfirmedException();
      }
      onError(e);
      setIsLoading(false);
    }
  }

  async function handleUserNotConfirmedException() {
    try {
      const newUser = await Auth.resendSignUp(fields.email);
      setIsLoading(false);
      setNewUser(newUser);
    } catch (e) {
      onError(e);
      setIsLoading(false);
    }
  }
1 Like