Is it safe to save an Array in localStorage?

0

Is it safe to save an array in localstorage? I read that one can convert the array to a string and then pause it but how sure is that the data will remain the same?

An alternative that I am considering is to use something like:

localStorage.setItem("datoA", data[0]);
localStorage.setItem("datoB",  data[1]);
....
....

would be much more code but only for security

    
asked by marco gomes 31.07.2017 в 12:36
source

3 answers

1

You can store arrays in the localStorage by first converting them to JSON and returning them to Array to read them.

localStorage.setItem("data", JSON.stringify(data));

//...
var data = JSON.parse(localStorage.getItem("data"));

With this you make sure that the data will remain the same when reading them.

Just keep in mind that as Diego says, everything you save in localStorage can be seen by the user.

    
answered by 31.07.2017 в 13:59
1

localStorage is the client's memory, which is free to be consulted by the client user. Keep in mind that the information you save there will be visible to anyone who knows how to enter the browser console, so you should not save sensitive information such as passwords or credit card numbers there, at least not before having encrypted them on the side of the browser. server.

Remember that on the web, for everything you want the client to do, you must provide the source code (if you do not want to use applets ), therefore, anyone who knows how to press CTRL+U will be able to read your logic. Learn to separate the tasks of your site and evaluate if the information is sensitive or not and if what you are serving in the client is critical for the operation.

    
answered by 31.07.2017 в 16:27
0

It is safe, in that the data is not lost unless you delete it (although it will depend on the browser settings ), commonly used to store authentication tokens, user preferences data, etc., remember that these data will be shared among all the open sections that point to that host in the browser, alternatively you can use sessionStorage is similar only that the data is persistent per window, instead localstorage will be unique and will remain until you delete it explicitly.

    
answered by 31.07.2017 в 17:14