简体   繁体   English

AWS Amplify:在 confirmSignUp 之后,自动登录用户的最佳做法是什么?

[英]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 .用户收到验证码后,用户输入验证码, Account Status变为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.使用注册过程中的emailpassword ,然后发送signIn操作以登录用户。
  2. Find a way combined method for both confirmSignUp and signIn , which would be something along the lines of confirmSignUpAndSignIn (If a method exists).confirmSignUpsignIn找到一种组合方法,这将类似于confirmSignUpAndSignIn (如果存在方法)。 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.我查看了AWS 文档和问题 Amplify-js Github Repo,但只发现其他人也有类似的困境,但没有明显的解决方案。
  3. Use the AWS Amplify Hub (Auth Listener) , but there aren't any events emitted when the user confirms sign up.使用AWS Amplify Hub (Auth Listener) ,但当用户确认注册时没有发出任何事件。 It would make sense that confirmSignUp would emit an event, but it doesn't. confirmSignUp会发出一个事件是有道理的,但事实并非如此。 (See Below) (见下文)

AWS Hub Listener: AWS 中心侦听器:

I'm using the AWS Amplify Hub (Auth listener) and the only events that are emitted are the following from the docs:我正在使用AWS Amplify Hub(Auth 侦听器)并且发出的唯一事件是来自文档的以下内容:

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使用aws-amplify@4.3.29 ,您可以通过启用autoSignIn功能并使用集线器侦听事件来实现它,详细信息在此 github 问题

As of Amplify JS API v4.3.29 it is now possible.从 Amplify JS API v4.3.29 开始,现在可以了。 Simply include the autoSignIn attribute in the signUp method.只需在 signUp 方法中包含autoSignIn属性。

 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 MFA等边缘情况的解决方案可以参见之前提到的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:我遇到了同样的挑战,在查看了 Amplify API 文档和源代码之后,我发现简单的解决方案是最好的:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AWS Amplify Sign In `currentAuthenticatedUser` 在登录后返回 `User is not authenticated` - AWS Amplify Sign In `currentAuthenticatedUser` returns `User is not authenticated` After Sign In 如何使用 AWS Amplify 和 React 在注册后让用户登录 - How to sign user in after registration using AWS Amplify and React AWS Amplify 如何在运行“amplify pull”命令后登录到管理 UI - AWS Amplify how to sign into Admin UI after running "amplify pull" command 删除用户后 AWS Amplify Auth.signIn 仍在工作 - AWS Amplify Auth.signIn is still working after user is deleted AWS Amplify + React 身份验证问题(在 Auth.signIn() 登录后未获得 JWT 值) - AWS Amplify + React Authentication Issue (Not getting JWT value after Auth.signIn() on Sign) AWS Amplify:如何在一段时间不活动后使用户 session 过期? - AWS Amplify: How to expire user session after certain time of inactivity? 将 AWS S3 数据添加到现有 Android 应用程序的最佳方法是什么 - AWSJDK、Amplify 或没有 Amplify 框架的 AWS Android JDK? - What is best way to add AWS S3 data gets to an existing Android app - AWSJDK, Amplify, or AWS Android JDK without Amplify framework? 为什么当用户登录或确认注册帐户反应本机 aws 放大时,用户会自动注销? - Why does user gets automatically logged out when user is logged in or confirms registered account react native aws amplify? AWS-Amplify,如何在角度登录后重定向到另一个 URL? - AWS-Amplify, How do I redirect to another URL after sign-in in angular? 在 AWS Amplify 中创建不同的用户类型 - Creating different User Types in AWS Amplify
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM