问题描述
那时我尝试调用更改密码API时,在Cognito用户池中收到“用户未通过身份验证”错误。
解决方法
嗨,我也为此苦苦挣扎,终于得到了这个解决方案。
您应该获得经过身份验证的用户
export const GetAuthenticatedUser = () => {
const userPool = new CognitoUserPool({
UserPoolId: process.env.OGNITO_POOL_ID,ClientId: process.env.COGNITO_CLIENT_ID
});
return userPool.getCurrentUser();
};
然后你应该调用 getSesion() 方法并尝试在其回调中更改密码,否则它不起作用。
export const ChangePassword = (data) => {
return new Promise((resolve,reject) => {
const currentUser = GetAuthenticatedUser();
if (currentUser) {
currentUser.getSession(function (err,session) {
if (err) {
reject(err);
} else {
currentUser.changePassword(data.old_password,data.password,function (
err,res
) {
if (err) {
reject(GetCognitoError(err.code + '_ChangePassword'));
} else {
resolve(res);
}
});
}
});
} else {
reject('User is not authenticated')
}
});
};