node.js - AWS Cognito Node.JS 用户身份验证返回未知问题
问题描述
我将 AWS Cognito 与 Node.JS 一起使用。
我已成功注册和验证用户,但身份验证返回“未知错误,来自 fetch 的响应正文未定义”。
我正在使用 node-fetch 模块和 amazon-cognito-identity-js(在下面的代码中设置为 var AWSCognito)。用户未处于需要更改密码并已验证的状态。
其他人有没有遇到过这种情况,你是如何解决这个问题的?提前感谢任何指导....
这是我的代码,我的完整模块在 npm 上作为 iditawsutils :
exports.authCognitoUser = function(theUserPoolID, theClientID, userName, userPassword) {
var authenticationData = {
Username : userName,
Password : userPassword
};
var authenticationDetails = new AWSCognito.AuthenticationDetails(authenticationData);
var poolData = { UserPoolId : theUserPoolID,
ClientId : theClientID
};
var userPool = new AWSCognito.CognitoUserPool(poolData);
var userData = {
Username : userName,
Pool : userPool
};
console.log('authentication details: ',authenticationDetails);
var cognitoUser = new AWSCognito.CognitoUser(userData);
cognitoUser.authenticateUser(authenticationDetails, {
onSuccess: function (result) {
console.log('access token + ' + result.getAccessToken().getJwtToken());
console.log('id token + ' + result.getIdToken().getJwtToken());
console.log('refresh token + ' + result.getRefreshToken().getToken());
return result;
},
onFailure: function(err) {
console.log(err.message || JSON.stringify(err));
return err;
},
});
}
//来自控制台日志:
authentication details: AuthenticationDetails {
validationData: {},
authParameters: {},
username: 'thesmarterstuff',
password: 'passW0rd!’ }
未知错误,来自 fetch 的响应正文是:未定义
解决方案
在 onFailure 块中使用以下内容来查找有关错误的更多详细信息。
onFailure: function(err) {
console.log(new Error().stack);
console.log(err.message || JSON.stringify(err));
},
如果您在 Client.js 的 fetch 行中发现错误,那么这可能是因为当前 NodeJS SDK 和大多数其他 SDK 不支持默认的 USER_SRP_AUTH。
您可以通过在 Client.js 中添加 console.log 来检查
console.log(this.endpoint);
console.log(options);
登录到您的 AWS 账户,并确保您已选中选项 - 启用基于应用程序的身份验证的用户名-密码(非 SRP)流程 (USER_PASSWORD_AUTH)
然后,在您的代码中使用以下设置更新它。
cognitoUser.setAuthenticationFlowType('USER_PASSWORD_AUTH');
推荐阅读
- node.js - 在 jest supertest 中开始集成测试之前清理测试数据库
- android - 如何从 Firebase 数据库侦听器将项目添加到 ArrayList
- c# - 从另一个 NavPage 的 ListView 将 SelectedItem 绑定到根 NavPage UI 不起作用
- powerbi - 如何通过过滤 Power Bi dax 查询中的每个日期来获得最终状态
- rabbitmq - rabbitmq-server 启动失败
- php - 如何正确管理 PHP 会话 cookie 过期?
- html - 是否可以在响应式微调器中具有与 CSS 中的父元素相关的动态边框宽度?
- android - SwipeDismissBehavior 动画问题
- gams-math - 是否可以在集合定义(GAMS)中使用标量?
- android - SpannableString 不起作用 - 显示未更改的数据