我一直在尝试使用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);
});
预期:
实际:
User.session()不是函数。
答案 0 :(得分:0)
我在Angular有一个示例(可能与React的代码相同):
cognitoUser.authenticateUser(authenticationDetails, {
onSuccess: result => {
console.log(err);
...
},
onFailure: err => {
console.log(err);
...
},
newPasswordRequired: (userAttributes, requiredAttributes) => {
subscriber.error({
reason: 'FORCE_CHANGE_PASSWORD',
user: {
username: user
}
});
}
});