gpt4 book ai didi

javascript - 如何使用 Amplify with Javascript 返回 AWS 用户池中的用户状态?

转载 作者:行者123 更新时间:2023-11-29 10:29:18 24 4
gpt4 key购买 nike

我的用户池中有一些用户: enter image description here

如何使用 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。

希望对您有所帮助。

关于javascript - 如何使用 Amplify with Javascript 返回 AWS 用户池中的用户状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50400512/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com