如何使用Amplify返回该特定用户的状态?我有用户的this.state.email.
这是我的handleSubmit函数的样子:
handleSubmit = async event => {
event.preventDefault();
this.setState({ isLoading: true });
try {
const newUser = await Auth.signUp({
username: this.state.email,
password: this.state.password,
});
this.setState({ newUser });
} catch (e) {
if (e.name === 'UserNotConfirmedException') {
alert(
'Looks like your account isn\'t confirmed! ' +
'Please check your email to find the confirmation code.',
);
// await Auth.resendSignUp(this.state.email);
this.setState({
newUser: {
username: this.state.email,
password: this.state.password,
},
});
} else if (e.name === 'UsernameExistsException') {
// how to check if unconfirmed?
if ('not sure what to put here') {
alert(
'Looks like your account isn\'t confirmed! ' +
'Please check your email to find the confirmation code.',
);
// bring up confirmation page
} else {
alert(
'Looks like you already have an account! ' +
'Please log in with your current password.',
);
this.props.history.push('/login');
}
} else {
alert(e.message);
}
}
this.setState({ isLoading: false });
}
有任何想法吗?
解决方法:
我不是您真正想要的是什么,但是如果您想找到一种方法来检索有关用户状态的信息以管理特定的工作流程,则必须检查ChallengeName和ChallengeParam.例如,当您登录一个新用户auth.signIn时,返回一个cognito用户:如果未确认该用户,则该用户将具有一个ChallengeName和ChallengeParm属性,您可以在代码中进行验证,例如:我从在控制台上输入了临时密码,因此他处于需要输入新密码的状态,根据我执行的代码,它可以完成新密码的工作流程
return fromPromise(Auth.signIn(username, password)).pipe(
tap(user => {
console.log('signIn Methode', user)
if (user.challengeName === 'NEW_PASSWORD_REQUIRED') {
this.navigate(['/completePassword']);
}
}), catchError(..){ }
在下面,您可以看到我从console.log显示的答案
user with “NEW PASSWORD REQUIRED STATUS”
如果确认用户,您将不会看到challengeName / challangeParm.
希望这会帮助你.