简体   繁体   中英

AWS Amplify: After confirmSignUp, what is the best practice to automatically Sign In a user?

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM