8

Overview:

After the user receives a Verification Code, the user enters the Verification Code, and the Account Status becomes CONFIRMED. Now that the sign up process is completed, I want to automatically sign in the user in after. It seems inefficient and redundant to redirect the user to the Sign In page to have user enter their information and sign in.

Possible Options:

  1. Use the email and password from the sign up process and then dispatch the signIn action to sign in the user.
  2. Find a way combined method for both confirmSignUp and signIn, which would be something along the lines of confirmSignUpAndSignIn (If a method exists). I have looked through the AWS Docs and through the issues Amplify-js Github Repo, but have only found that others have had a similar dilemma with no apparent resolution.
  3. Use the AWS Amplify Hub (Auth Listener), but there aren't any events emitted when the user confirms sign up. It would make sense that confirmSignUp would emit an event, but it doesn't. (See Below)

AWS Hub Listener:

I'm using the AWS Amplify Hub (Auth listener) and the only events that are emitted are the following from the docs:

case 'signIn':
    logger.error('user signed in'); //[ERROR] My-Logger - user signed in
    break;
case 'signUp':
    logger.error('user signed up');
    break;
case 'signOut':
    logger.error('user signed out');
    break;
case 'signIn_failure':
    logger.error('user sign in failed');
    break;
case 'configured':
    logger.error('the Auth module is configured');
jefelewis
  • 1,850
  • 1
  • 27
  • 63
  • Have you looked at the code for the [ConfirmSignUp](https://github.com/aws-amplify/amplify-js/blob/aws-amplify%403.0.24/packages/aws-amplify-vue/src/components/authenticator/ConfirmSignUp.vue) component? In the later versions the user is automatically signed in after confirmation - look for confirm()... – Lukas Rossa Nov 16 '20 at 09:00
  • 1
    @LukasRossa That works for their UI components library, their bare calls don't automatically sign a person in. – Munib Sep 29 '21 at 17:42

3 Answers3

1

I had the same challenge, after looking around in the Amplify API docs and the source code, I found that the simple solution is the best:

try {
  // try to confirm the code
  await Auth.confirmSignUp(formValues.email, formValues.confirmationCode);
  // If successful, sign user in
  await Auth.signIn({
    username: formValues.email,
    password: formValues.password,
  });
} catch (error) {
  console.log('error', error);
}
Tobi
  • 31,405
  • 8
  • 58
  • 90
1

With aws-amplify@4.3.29 you can achieve it by enabling autoSignIn feature and listening for event with the Hub, details are in this github issue

mmaksitaliev
  • 126
  • 1
  • 8
1

As of Amplify JS API v4.3.29 it is now possible. Simply include the autoSignIn attribute in the signUp method.

 Auth.signUp({
  username: 'xxxxxx',
  password: '*********,
  attributes: {
    email: 'xxxxxxxxxx'
  },
  autoSignIn: {
    enabled: true
  }
})

Solutions for edge cases, such as MFA, can be seen in the previously mentioned GitHub issue

Zog
  • 1,094
  • 2
  • 11
  • 24