[英]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:可能的选择:
email
and password
from the sign up process and then dispatch the signIn
action to sign in the user.使用注册过程中的email
和password
,然后发送signIn
操作以登录用户。confirmSignUp
and signIn
, which would be something along the lines of confirmSignUpAndSignIn
(If a method exists).为confirmSignUp
和signIn
找到一种组合方法,这将类似于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,但只发现其他人也有类似的困境,但没有明显的解决方案。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.