# Signup with AWS Cognito

**URL:** <https://discourse.sst.dev/t/signup-with-aws-cognito/130>\
**Category:** Chapter Comments\
**Created:** [May 9, 2018, 7:53pm UTC](https://discourse.sst.dev/t/signup-with-aws-cognito/130 "2018-05-09T19:53:46Z")\
**Posts on this page:** 12\
**Page:** 4

<div class="post-metadata">

**Author:** ![HannahStahl](https://avatars.discourse-cdn.com/v4/letter/h/ed655f/32.png) [@HannahStahl](https://discourse.sst.dev/u/HannahStahl)\
**Post date:** [October 27, 2019, 10:43pm UTC](https://discourse.sst.dev/t/signup-with-aws-cognito/130/61 "2019-10-27T22:43:21Z")

</div>

Oh great! I completely overlooked that extra credit chapter. I’ll follow that tutorial and then try to set up Google afterward. I’ll be sure to send over my steps if I’m successful in getting it to work!

---

<div class="post-metadata">

**Author:** ![fortj](https://avatars.discourse-cdn.com/v4/letter/f/59ef9b/32.png) [@fortj](https://discourse.sst.dev/u/fortj)\
**Post date:** [December 27, 2019, 5:53am UTC](https://discourse.sst.dev/t/signup-with-aws-cognito/130/62 "2019-12-27T05:53:17Z")

</div>

Has anyone figured out a way to incorporate MFA into this signup flow?

I have got the sign up and verify working, but looking at adding MFA to both the sign up and login, but have not been able to work it out yet. Would appreciate any guidance.

Thanks.

---

<div class="post-metadata">

**Author:** ![jayair](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/jayair/32/9_2.png) [@jayair](https://discourse.sst.dev/u/jayair)\
**Post date:** [January 12, 2020, 3:33am UTC](https://discourse.sst.dev/t/signup-with-aws-cognito/130/63 "2020-01-12T03:33:50Z")

</div>

Have you tried the MFA portion in the Amplify docs - [https://aws-amplify.github.io/docs/js/authentication#enabling-mfa](https://aws-amplify.github.io/docs/js/authentication#enabling-mfa). We’ve used it internally and it works.

We might add this to the guide at some point.

---

<div class="post-metadata">

**Author:** ![fortj](https://avatars.discourse-cdn.com/v4/letter/f/59ef9b/32.png) [@fortj](https://discourse.sst.dev/u/fortj)\
**Post date:** [January 22, 2020, 11:14pm UTC](https://discourse.sst.dev/t/signup-with-aws-cognito/130/64 "2020-01-22T23:14:48Z")

</div>

Thanks, I couldn’t get amplify to do everything for MFA enforced registration. In the end I ended up using Amplify and qrcode.react to generate the QR, then a lambda function to validate the submitted TOTP code. This validation function is reused for login. That way the MFA validation is handled ‘server’(less)-side in the Lambda, rather than by the front-end.

For the purposes of the registration I was also hoping that I would be able to use the code quick enough to both confirm MFA and log the user in. But the TOTP is single use, so there is no way around that.

---

<div class="post-metadata">

**Author:** ![jayair](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/jayair/32/9_2.png) [@jayair](https://discourse.sst.dev/u/jayair)\
**Post date:** [January 26, 2020, 6:22pm UTC](https://discourse.sst.dev/t/signup-with-aws-cognito/130/65 "2020-01-26T18:22:07Z")

</div>

Yeah that makes sense. We did something similar but did the validation on the frontend.

---

<div class="post-metadata">

**Author:** ![davemrunal](https://avatars.discourse-cdn.com/v4/letter/d/e56c9b/32.png) [@davemrunal](https://discourse.sst.dev/u/davemrunal)\
**Post date:** [March 13, 2020, 2:24am UTC](https://discourse.sst.dev/t/signup-with-aws-cognito/130/67 "2020-03-13T02:24:57Z")

</div>

Why use useFormFields hook here to store email, pwd, confirmPwd, confirmCode instead of storing them in the state?

---

<div class="post-metadata">

**Author:** ![jeongwoo](https://avatars.discourse-cdn.com/v4/letter/j/ce7236/32.png) [@jeongwoo](https://discourse.sst.dev/u/jeongwoo)\
**Post date:** [March 14, 2020, 10:40am UTC](https://discourse.sst.dev/t/signup-with-aws-cognito/130/68 "2020-03-14T10:40:13Z")

</div>

Because code reuse! don’t repeat, reuse code applied useFormFileds(Custom Hook)

---

<div class="post-metadata">

**Author:** ![smoq](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/smoq/32/1163_2.png) [@smoq](https://discourse.sst.dev/u/smoq)\
**Post date:** [April 20, 2021, 1:01pm UTC](https://discourse.sst.dev/t/signup-with-aws-cognito/130/71 "2021-04-20T13:01:02Z")

</div>

Hi everyone  
not sure if the case with handling not confirmed user was solved so pasting the way I handled it.

In case user wants to submit existing email I am showing info that the user exists and redirecting him/her to Log in page. Then when user tries to log in and is not confirmed alert with information that user is not confirmed is displayed and I am sending new code and displaying confirmation form.

---

<div class="post-metadata">

**Author:** ![arielkark-versent](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/arielkark-versent/32/1178_2.png) [@arielkark-versent](https://discourse.sst.dev/u/arielkark-versent)\
**Post date:** [June 7, 2021, 1:42pm UTC](https://discourse.sst.dev/t/signup-with-aws-cognito/130/72 "2021-06-07T13:42:03Z")

</div>

Hi, are you please able to share your solution?

---

<div class="post-metadata">

**Author:** ![PieCrafted](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/piecrafted/32/1226_2.png) [@PieCrafted](https://discourse.sst.dev/u/PieCrafted)\
**Post date:** [September 9, 2021, 10:04pm UTC](https://discourse.sst.dev/t/signup-with-aws-cognito/130/73 "2021-09-09T22:04:48Z")

</div>

Are you able to share your solution?

This is what I have so far:

```auto
async function handleSubmit(event) {
      event.preventDefault();
    
      setIsLoading(true);
    
      try {
        const newUser = await Auth.signUp({
          username: fields.username,
          password: fields.password,
          attributes: {
            email: fields.email,
          }
        });
        setIsLoading(false);
        setNewUser(newUser);
      } catch (e) {
        if (e.name === "UsernameExistsException") {
          onError("Username already exists! Please try a new one or login if this is your username.");
          setIsLoading(false);
        } else {
          onError(e);
          setIsLoading(false);
        }
      }
    }

```

---

<div class="post-metadata">

**Author:** ![nookale](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/nookale/32/365_2.png) [@nookale](https://discourse.sst.dev/u/nookale)\
**Post date:** [January 9, 2023, 11:15pm UTC](https://discourse.sst.dev/t/signup-with-aws-cognito/130/74 "2023-01-09T23:15:21Z")

</div>

Hi, I have made this work:

```auto
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);
    }
  }

```

---

<div class="post-metadata">

**Author:** ![ipsherman](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/ipsherman/32/1449_2.png) [@ipsherman](https://discourse.sst.dev/u/ipsherman)\
**Post date:** [June 13, 2023, 2:38pm UTC](https://discourse.sst.dev/t/signup-with-aws-cognito/130/75 "2023-06-13T14:38:27Z")

</div>

Great solution! I made a slight change to the `handleUsernameExistsException()` function, to take into account the case where the user entered the wrong password:

```javascript
  async function handleUsernameExistsException() {
    try {
      await Auth.signIn(fields.email, fields.password);
      userHasAuthenticated(true);
      nav("/");
    } catch (e) {
      if (e.name === "UserNotConfirmedException") { // user is registered but not confirmed
        handleUserNotConfirmedException();
      } else if (e.name === "NotAuthorizedException") { // user is registered and confirmed, but entered the wrong password
        alert("It looks like you are already registered. Redirecting to Login...");
        nav("/login");
      } else {
        onError(e);
      }
      setIsLoading(false);
    }
  }

```

Nothing crazy, just alerts the user that they entered the wrong password and redirects to `/login`.

[Previous page](https://discourse.sst.dev/t/signup-with-aws-cognito/130.md?page=3)
