amazon-web-services - 使用 Amazon Web Services Cognito Userpool 时如何记住和自动刷新登录 token ?

标签 amazon-web-services amazon-cognito

我构建了一个使用 AWS Cognito 和 Userpool 功能的网站。

如果我离开页面,登录就会被忘记,一小时后 token 就会过期。

我希望在用户关闭浏览器并返回时记住登录名。

我还希望 auth token 自动刷新,而不是在一小时后给出错误。

我怎样才能做到这一点?

最佳答案

用户池 token 保存到本地存储。并且在用户池对象上调用 getCurrentUser 时,您可以检索最后一个经过身份验证的用户对象。之后,调用 getSession 应使用刷新 token 来检索已过期的新访问 token ,如下例所示。在 getSession 的回调中,您应该有一个有效的 session 。

var poolData = {
    UserPoolId : '...', // Your user pool id here
    ClientId : '...' // Your client id here
};
var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData);
var cognitoUser = userPool.getCurrentUser();

if (cognitoUser != null) {
    cognitoUser.getSession(function(err, session) {
        if (err) {
           alert(err);
            return;
        }
        console.log('session validity: ' + session.isValid());
        //You should have a valid session here
    });
}

关于amazon-web-services - 使用 Amazon Web Services Cognito Userpool 时如何记住和自动刷新登录 token ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42002953/

相关文章:

amazon-web-services - Kubernetes "unauthorized: authentication required"

node.js - 在 AWS lambda 上列出 cognito 用户池用户

amazon-web-services - AWS Cognito 自定义页面的空白页面

android - 如何搜索 Amazon Cognito 身份?

amazon-web-services - 没有 RDS 的 AWS 中的唯一序列号生成器?

python-3.x - 无法删除安全组: An error occurred (DependencyViolation) when calling the DeleteSecurityGroup operation

amazon-web-services - 使用 Cloudfront 设置 AWS ELB 而不是单个 EC2 实例有什么意义?

swift - 如何使用 AWSS3TransferUtility 将带有用户元数据的音频文件上传到 S3

swift - 使用 Swift 验证 AWS Cognito token

javascript - cognitoUser.signOut() 未注销用户