【发布时间】:2019-11-06 22:16:26
【问题描述】:
我一直在尝试使用 Amplify JS 进行身份验证,并且能够在 user.challengeName === 'NEW_PASSWORD_REQUIRED' 时登录和更改密码,并且我的 Approuter 中有 componentDidMount(),其中所有路由站点。当导航到更改密码屏幕时componentDidMount() 没有调用。
登录条件:
if (user.challengeName === 'NEW_PASSWORD_REQUIRED') {
this.props.history.push('/ChangePassword')}
在 Approuter 中:
async componentDidMount() {
try {
const session = await Auth.currentSession();
this.setAuthStatus(true);
console.log(session);
const user = await Auth.currentAuthenticatedUser();debugger;
this.setUser(user);
} catch(error) {
if (error !== 'No current user') {
console.log(error);
}
}
更改密码:
let user = Auth.currentAuthenticatedUser();
Auth.changePassword(
user,
values.tempPassword,
values.newPassword
).then(user => {
// at this time the user is logged in if no MFA required
console.log(user);
}).catch(e => {
console.log(e);
});
预期:
- 用户必须是 Cognito 对象,更改密码应更新并确认用户。
实际:
User.session() 不是函数。
【问题讨论】:
-
NEW_PASSWORD_REQUIRED已实现(github.com/aws-amplify/amplify-js/issues/2602),需要使用回调。
标签: javascript reactjs authentication amazon-cognito