适用于javascript webapp的AWS cognito。如何检查认知令牌是否已过期

时间:2016-06-05 12:22:34

标签: javascript amazon-web-services amazon-cognito

所以我有一个基本的webapp,我试图使用AWS cognito创建用户访问控制。我有一个自定义授权提供商,并输入正确的用户名密码。我在登录页面中执行以下操作:

//Successful Login
  var creds = AWS.config.credentials;
  creds.params.IdentityId = output.identityId;
  creds.params.Logins = {'cognito-identity.amazonaws.com': output.token};	
							
//Store token in browser cache
  localStorage.setItem('token', output.token);
  localStorage.setItem('id', output.identityId);
							
//Launch dashboard 
  window.location = "./index.html";

一旦我将用户重定向到仪表板,我就会触发一个onLoad函数来检查用户是否具有正确的登录凭据而不是已过期的凭据。使用这个:

//read browser cache
  var id = localStorage.getItem('id');
  var token = localStorage.getItem('token');
			
//validate session
			
  AWS.config.region = 'ap-northeast-1';
  AWS.config.credentials = new AWS.CognitoIdentityCredentials({
    IdentityPoolId: id,
	Logins: {'cognito-identity.amazonaws.com': token}
  });
			
//check if session is still active
  if(AWS.config.credentials.expired) window.location = "./session-expire.html";

问题是过期属性始终为true。不管我做什么。你们如何检查凭证是否有效?

提前致谢, 拉詹

1 个答案:

答案 0 :(得分:1)

在您提供的代码示例中,您永远不会获取凭据。您设置了凭据提供程序,但不使用它调用任何服务,或明确尝试获取凭据。

其次,凭据不会在页面加载中持久存在。

这将导致您的代码示例中的凭据已过期。

这个问题可能对您有帮助,只需用您自己的开发人员验证流程替换facebook即可。 AWS.config.credentials are null between page requests