admin管理员组

文章数量:1410689

I'm using Amazon Cognito for user login for a website with the Javascript SDK which uses local storage to save the user's credentials which are used for things like auto login. This doesn't work when running from a local file on your puter.

Is it possible to direct the Javascript SDK to save user credentials through some other means instead of local storage when running a website locally?

I've seen references to changing the storage object but I can't find any samples on how to actually implement a custom storage solution.

I'm using Amazon Cognito for user login for a website with the Javascript SDK which uses local storage to save the user's credentials which are used for things like auto login. This doesn't work when running from a local file on your puter.

Is it possible to direct the Javascript SDK to save user credentials through some other means instead of local storage when running a website locally?

I've seen references to changing the storage object but I can't find any samples on how to actually implement a custom storage solution. https://github./aws/amazon-cognito-identity-js/pull/363

Share Improve this question asked Feb 5, 2018 at 21:18 Berry BlueBerry Blue 16.6k22 gold badges77 silver badges147 bronze badges 2
  • I know this doesn't necessarily answer your question, but why not just provide a lightweight server to serve the site up locally over localhost? Would your application usually be downloaded by end users and run directly from a file? It sounds like you're needing this for testing and development, and serving up the file over localhost would more closely match your end user's experience. – mootrichard Commented Feb 14, 2018 at 18:58
  • Thanks for your suggestion. The issue is the HTML file is being served in an Adobe Extension where local storage isn't available but you have the same issue if you load a local file using a browser like Safari. – Berry Blue Commented Feb 14, 2018 at 19:40
Add a ment  | 

1 Answer 1

Reset to default 5 +25

To answer your exact question

As shown in the pull request you linked to you can now specify a Storage object for the pool to use. There are two built in Storage objects according to MDN: localStorage and sessionStorage. You may be able to use sessionStorage as is (I haven't tried it). It will survive page reloads but is cleared when the page/tab is closed. New tabs get a new sessionStorage object.

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

If that doesn't work you'll need to build an object that implements the Storage API. It's pretty simple and would not take much to do. There are 5 total methods and most can be mapped 1:1 with a simple Object

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

Here is an NPM package that implements the Storage API in memory. Downside is that page refreshes would clear it.

As you can see from the source code it's not very plicated.

However

In my opinion a better alternative would be to use a very simple server like serve or a Web Server for Chrome to serve the files over http so that localStorage (and many other parts of the page) work as you would expect.

本文标签: javascriptSaving Amazon Cognito user credentials without local storageStack Overflow