gpt4 book ai didi

javascript - 在没有本地存储的情况下保存 Amazon Cognito 用户凭证

转载 作者:数据小太阳 更新时间:2023-10-29 05:24:39 25 4
gpt4 key购买 nike

我使用 Amazon Cognito 通过 Javascript SDK 为网站进行用户登录,该 SDK 使用本地存储来保存用于自动登录等操作的用户凭据。从计算机上的本地文件运行时,这不起作用。

在本地运行网站时,是否可以指示 Javascript SDK 通过其他方式而不是本地存储来保存用户凭据?

我看过更改存储对象的引用资料,但我找不到任何关于如何实际实现自定义存储解决方案的示例。 https://github.com/aws/amazon-cognito-identity-js/pull/363

最佳答案

回答你的确切问题

pull request you linked所示您现在可以指定 Storage供池使用的对象。根据 MDN,有两个内置存储对象:localStoragesessionStorage .您可以使用 sessionStorage原样(我没试过)。它会在页面重新加载后继续存在,但会在页面/选项卡关闭时被清除。新标签获得新的 sessionStorage对象。

let pool = new CognitoUserPool({ 
UserPoolId,
ClientId,
storage: window.sessionStorage
});

如果这不起作用,您需要构建一个实现 Storage 的对象应用程序接口(interface)。这很简单,不需要做太多事情。总共有 5 种方法,大多数可以使用简单的 Object

进行 1:1 映射
Storage.key(int n); // Return the Nth key
Storage.getItem(string key) // return the value for given key
Storage.setItem(string key, string value) // store a value at key
Storage.removeItem(string key) // remove the value for key
Storage.clear() // delete all values

这是一个实现 Storage 的 NPM 包API memory .缺点是页面刷新会清除它。

source code可以看出不是很复杂。

然而

在我看来,更好的选择是使用一个非常简单的服务器,如 serveWeb Server for Chrome通过 http 提供文件,以便 localStorage (以及页面的许多其他部分)按您预期的方式工作。

关于javascript - 在没有本地存储的情况下保存 Amazon Cognito 用户凭证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48631647/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com