我有一个使用AWS API网关和AWS Cognito身份验证的网页。网页中的大多数按钮都调用Api并发送或检索数据。这是我在每次单击按钮时使用的代码
getAuthenticateUser().getSession(function(err,session){
if (err) {
console.log("Error"+err);
return;
}
if(session.isValid())
{
$.ajax({
url: URL,
headers:
{
'Authorization':session.getIdToken().getJwtToken(),
'Content-Type':'application/json'
},
method: 'GET',
dataType: 'json',
success: function(data){
// Execute Function Need to Execute on Success
}
,
error: function (xhr, textStatus, errorThrown) {
console.log(errorThrown);
}
});
}
if(!session.isValid())
{
console.log("Session is not valid..!");
//Please Login to Continue
}
});
这工作正常,没有问题。但问题是,每次执行它都需要一些时间,感觉就像每次调用都会有一个新会话一样,即使之前的会话没有过期。如何仅在会话过期时使用get new Session?如果前一个会话未过期,只需全局检查其有效性,并仅获取我需要的新授权标头。。?
因为在我的代码中,一旦用户按下一个按钮并发送数据id,同一用户在30秒后按下另一个按钮,我就会再次获得新会话,对吗。。?它真的让我的网站性能很差。
有没有办法解决这个问题。。?
这是my getAuthenticationUser()代码
function getAuthenticateUser()
{
return userPool.getCurrentUser();
}
这是Javascript页面中的全局变量
var poolData = {
UserPoolId : 'usXXXXXXXXXXOv3jSL',
ClientId : 'XXXXXXXXXXka8g'
};
var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData);