我目前在使用AWS作为后端的应用程序时遇到了大问题。当我测试应用程序时,第一次在手机上安装应用程序或在浏览器上提供应用程序时出现错误。在登录后第一次立即我必须从DynamoDB加载用户的数据,但是我收到此错误
POST https://cognito-identity.us-east-1.amazonaws.com/ 400() 错误:配置中缺少凭据
所以我安慰了我发现它全部设置为null的凭据。首次刷新页面后(关闭打开手机上的应用程序)。我注意到凭据不再为空,但仍然没有处理DynamoDb查询。
对于第三次刷新,一切都恢复正常工作。这使我的应用程序的用户体验非常糟糕,所以我必须找到解决方案。
这是我的登录功能
login(username, password) {
return new Promise((resolve, reject) => {
let user = this.cognito.makeUser(username);
let authDetails = this.cognito.makeAuthDetails(username, password);
user.authenticateUser(authDetails, {
'onSuccess': (result:any) => {
var logins = {};
var loginKey = 'cognito-idp.' +
aws_cognito_region +
'.amazonaws.com/' +
aws_user_pools_id;
logins[loginKey] = result.getIdToken().getJwtToken();
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
'IdentityPoolId': aws_cognito_identity_pool_id,
'Logins': logins
});
AWS.config.credentials.get((err) => {
if (err) {
return reject(err);
}
this.isAuthenticated().then(() => {
resolve();
}).catch((err) => {
console.log('auth session failed');
});
});
},
'onFailure': (err:any) => {
console.log('authentication failed');
reject(err);
},
newPasswordRequired: (userAttributes, Password) => {
// User was signed up by an admin and must provide new
// password and required attributes, if any, to complete
// authentication.
var newPassword;
console.log(newPassword);
delete userAttributes.email_verified;
delete userAttributes.phone_number_verified;
let addModal = this.modalCtrl.create(ChangePasswordPage );
addModal.onDidDismiss(data => {
newPassword = data;
user.completeNewPasswordChallenge(newPassword, userAttributes, {
onSuccess: (result:any) => {
var logins = {};
var loginKey = 'cognito-idp.' +
aws_cognito_region +
'.amazonaws.com/' +
aws_user_pools_id;
logins[loginKey] = result.getIdToken().getJwtToken();
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
'IdentityPoolId': aws_cognito_identity_pool_id,
'Logins': logins
});
AWS.config.credentials.get((err) => {
if (err) {
return reject(err);
}
});
this.isAuthenticated().then(() => {
resolve();
}).catch((err) => {
console.log('auth session failed');
});
},
authSuccess: function (result){
console.log('In the AuthSuccess.');
},
onFailure: function(err) {
console.log('In the Error '+err);
}
});
console.log(newPassword);
})
addModal.present();
console.log(newPassword);
}
});
});
}
这是我的dynamoDb函数:
refreshTasks() {
console.log( AWS.config.credentials);
return new Promise((resolve, reject) => {
var d = new Date();
var s= [this.pad(d.getDate()), this.pad(d.getMonth()+1), d.getFullYear()].join('/');
console.log(s);
const params = {
'TableName': this.dailyTable,
'KeyConditionExpression':
"#userId = :userId AND d = :day",
'ExpressionAttributeNames': {
'#userId': 'userId'
},
'ExpressionAttributeValues': {
':userId': AWS.config.credentials.identityId,
':day':s.toString()
},
'ScanIndexForward': false
};
this.db.getDocumentClient().query(params).promise().then((data) => {
console.log("data loaded");
console.log(data);
resolve(data);
}).catch((err) => {
console.log(err);
});
});
}
答案 0 :(得分:0)
通过允许匿名访问移动中心项目来解决此问题。作为凭证延迟加载。并使用用户ID查询表,直到凭据准备就绪。对于遇到同样问题的人来说,这是一个临时解决方案。