如何在浏览器中的各个页面之间保留Cognito身份 [英] How to persist Cognito identity across pages in browser

查看:70
本文介绍了如何在浏览器中的各个页面之间保留Cognito身份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用开发人员验证的身份在客户端浏览器上通过Cognito进行验证。当页面加载(或刷新)时,只要对象未过期(我认为它持续约一个小时),我希望我的应用程序记住身份。但是,我不知道如何从Cognito检索身份而不必再次通过开发人员身份验证。

I am authenticating through Cognito on client side browser using a developer authenticated identity. When my page loads (or is refreshed) I would like my application to remember the Identity for as long as the object is not expired (I think it lasts about an hour). However, I don't know how to retrieve the identity from Cognito without having to go through the developer authentication again.

这是页面加载时代码的作用:

Here is what the code does on page load:

var cognitoCredentials

$(document).ready(function() { 
    "use strict";

    cognitoParams = {
      IdentityPoolId: 'us-east-1:xxxxxxx'
    };

    cognitoCredentials = new AWS.CognitoIdentityCredentials(cognitoParams);
    AWS.config.credentials = cognitoCredentials;

});

在通过开发人员身份验证登录后:

And after logging in through the developer authentication:

cognitoCredentials.params.IdentityId = output.identityId;
cognitoCredentials.params.Logins = {
    'cognito-identity.amazonaws.com': output.token
};
cognitoCredentials.expired = true;

如果我已经登录,然后刷新页面,然后尝试重新登录,当我已经有一个
时,我试图获取身份的错误。错误:config(…)中缺少凭据NotAuthorizedException:缺少配置中的凭据
禁止访问身份'us-east-1:xxxxxxx'。

If I have already logged in, and then refresh the page, and try to log in again I get an error that I am trying to get an identity when I already have one Error: Missing credentials in config(…) NotAuthorizedException: Missing credentials in config "Access to Identity 'us-east-1:xxxxxxx' is forbidden."

但是,我不知道不知道如何访问它。如何检索凭据,以便刷新页面时可以检测到Cognito给出的以前的身份?

However, I don't know how to access it. How do I retrieve the credentials so that when the page is refreshed, I can detect the previous identity given by Cognito?

推荐答案

返回页面刷新时相同标识的唯一方法是使用用于初始化该标识的相同令牌。您可能想参考此问题,因为问题是类似(用开发人员验证过的身份流程中的OpenId Connect令牌替换Facebook令牌)。

The only way to get back to the same identity on page refresh would be to use the same token used to initialize that identity. You may want to refer to this question as the problems are similar (replacing the Facebook token with the OpenId Connect token from the developer authenticated identities flow).

要重申该问题的内容:SDK中的凭据将不会保留跨页面,因此您应该缓存要重用的令牌。

To reiterate what that question says: the credentials in the SDK will not be persisted across pages, so you should cache the token to be reused.

这篇关于如何在浏览器中的各个页面之间保留Cognito身份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆