amazon-web-services - Amazon-cognito-identity-js,获取 callback.newPasswordRequired 不是函数错误
问题描述
Amazon-cognito-identity-js,在尝试更改经过身份验证的用户的密码时出现“callback.newPasswordRequired 不是函数”错误
const poolData = {
UserPoolId: 'eu-central-1_XXXXXXX'
ClientId: '2xxxxxxxxxxxxxxxxxxo',
}
const authenticationData = {
Username: name,
Password: oldPassword,
}
const authenticationDetails = new AuthenticationDetails(authenticationData)
const userPool = new CognitoUserPool(poolData)
const cognitoUser = new CognitoUser({ Username: name, Pool: userPool })
cognitoUser.authenticateUser(authenticationDetails, {
onSuccess(result) {
cognitoUser.changePassword(oldPassword, newPassword, function (err, passwordChangeResult) {
if (err) {
console.warn(err.message || JSON.stringify(err))
}
console.warn(`Password change result: ${passwordChangeResult}`)
})
},
onFailure(err) {
console.warn(err)
},
})
return null
}
解决方案
似乎 Cognito 正在返回一个您尚未定义的 newPasswordRequired 回调。我会假设用户是由 Admin API 调用而不是通过 Sign Up 调用创建的。然后在登录时,系统会提示您重置密码。
取自这里 [1],您可以像这样实现 newPasswordRequired 回调。
cognitoUser.authenticateUser(authenticationDetails, {
onSuccess: function (result) {
// User authentication was successful
},
onFailure: function(err) {
// User authentication was not successful
},
mfaRequired: function(codeDeliveryDetails) {
// MFA is required to complete user authentication.
// Get the code from user and call
cognitoUser.sendMFACode(mfaCode, this)
},
newPasswordRequired: function(userAttributes, requiredAttributes) {
// User was signed up by an admin and must provide new
// password and required attributes, if any, to complete
// authentication.
// the api doesn't accept this field back
delete userAttributes.email_verified;
// store userAttributes on global variable
sessionUserAttributes = userAttributes;
}
});
[1] https://www.npmjs.com/package/amazon-cognito-identity-js
推荐阅读
- amazon-web-services - 如何使用 boto3 删除 MFA 保护存储桶上的 S3 对象/版本?
- html - 如何让这个容器粘在页面底部?
- java - Spring Webclient 有时不读取 204 响应
- parallel-processing - 翘曲散度
- python - 强制 python ssh 解释器使用本地数据
- machine-learning - Caffemodel 和 prototxt
- microsoft-graph-api - 有没有办法从办公室加载项任务窗格中打开日历项目?
- jquery - 尝试使用 Laravel 和 jquery 访问 null 类型值的数组偏移量
- javascript - 如何在 angularJS 指令中操作变量
- python - 如何理解二叉树中的递归