Home > Enterprise >  How sessionStorage or localStorage is stored across domains
How sessionStorage or localStorage is stored across domains

Time:06-02

I want to store data in sessionStorage or localStorage across domains, what should I do? Because the vue project needs to carry the data to the react project, my English is not very good, I hope you can understand what I mean

CodePudding user response:

I think this is by design. So I don't think you can do this.

"Data stored in sessionStorage is specific to the protocol of the page. In particular, data stored by a script on a site accessed with HTTP (e.g., http://example.com) is put in a different sessionStorage object from the same site accessed with HTTPS (e.g., https://example.com)."

"localStorage data is specific to the protocol of the document. In particular, for a site loaded over HTTP (e.g., http://example.com), localStorage returns a different object than localStorage for the corresponding site loaded over HTTPS (e.g., https://example.com)."

https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage

CodePudding user response:

Both storages are accessible by Javascript, so you can easily set your data in local storage and get the data, like this for example:

localStorage.setItem('data',YOUR_DATA_HERE)

you can retrieve the data from the other end as a response

and then you would be able to control your data the way you want in either ends (vue or react).

Edit: setting and getting data from storage should be done in the same domain, so to answer your question, it cant be done from 2 different domains, but you can send the data as a response to another end.

However, if you are storing an auth token, or a session id, it is recommended to not store such things in local storage or session storage, since these storages are accessible by JavaScript, then your user is vulnerable for attack (man in middle attack for example), a good way to do it is to store the session id in cookies tagged with httponly, and control it from backend only.

  • Related