简体   繁体   English

适用于javascript web应用程序的AWS Cognito。 如何检查Cognito令牌是否已过期

[英]AWS cognito for javascript webapp. How to check if cognito token has expired or not

So I have a basic webapp, and Im trying to create user access control using AWS cognito. 所以我有一个基本的webapp,我正在尝试使用AWS cognito创建用户访问控制。 I have a custom authorisation provider and upon entering correct username password. 我有一个自定义授权提供程序,并输入了正确的用户名密码。 I do the following in my login page: 我在登录页面中执行以下操作:

 //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"; 

Once I redirect the user to the dashboard, I trigger a onLoad function to check whether the user has correct login credentials and not expired ones. 将用户重定向到仪表板后,我将触发onLoad函数以检查用户是否具有正确的登录凭据而不是过期的凭据。 Using this: 使用这个:

 //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"; 

Problem is the expired property is always true. 问题是过期属性始终为true。 no matter what I do. 不管我做什么。 How do you guys check if the credentials are valid?. 你们如何检查凭据是否有效?

Thanks in advance, Rajan 在此先感谢Rajan

In the code samples you provided, you never acquire credentials. 在您提供的代码示例中,您永远不会获得凭据。 You set up the credentials provider, but do not call any services with it, or explicitly try to get the credentials. 您设置了凭据提供程序,但不使用它调用任何服务,也没有明确尝试获取凭据。

Secondly, the credentials are not persisted across page loads. 其次,凭据不会在页面加载之间持久保存。

This will lead to the credentials being expired in your code samples. 这将导致凭据在您的代码示例中过期。

This question may help you, just replace facebook with your own developer auth flow. 这个问题可能会对您有帮助,只需用您自己的开发人员身份验证流程替换facebook。 AWS.config.credentials are null between page requests 页面请求之间的AWS.config.credentials为null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM