Store Amazon Cognito user credentials without local storage

I use Amazon Cognito to log in a user for a site with a Javascript SDK that uses local storage to store user credentials that are used for things like automatic login. This does not work when running from a local file on your computer.

Is it possible to configure the Javascript SDK to save user credentials using other means instead of local storage when the website is launched locally?

I saw links to changing the storage object, but I can not find any examples of how to really implement a special storage solution. https://github.com/aws/amazon-cognito-identity-js/pull/363

+5
source share
1 answer

To answer your exact question

As shown in pull the request that you linked , you can now specify Storage for the pool. There are two built-in storage objects according to MDN: localStorage and sessionStorage . You can use sessionStorage as is (I have not tried it yet). It will save the page reload, but will be cleared when the page / tab is closed. New tabs get a new sessionStorage object.

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

If this does not work, you will need to create an object that implements the Storage API. It is quite simple and does not take much time. There are 5 general methods, and most of them can be matched 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 the NPM package that implements the Storage API in memory . The downside is that refreshing the page will clear it.

As you can see from the source code , this is not very difficult.

but

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

+2
source

Source: https://habr.com/ru/post/1275180/


All Articles