Nuru Salihu
Nuru Salihu

Reputation: 4948

How to use localStorage in Cordova mobile app?

Please I developed my mobile app and is primarily using localStorage for storing account info and some other info. The app has mobile and browser version.

For the browser version, I expect a user that is logged in to not be redirected to an auth page on browser restart. This works fine as my info are stored in localStorage. On the other hand, my mobile doesn't. User have to log in whenever he restart the app.

Please what option do I have? What option do developers use for the mobile storage? Do I have to use database? If so which one should use and where can I find a better documentation on this topic?

Note: I am using Cordova for my mobile app development framework.

Upvotes: 0

Views: 9993

Answers (3)

Zsolt
Zsolt

Reputation: 11

For some reason, localStorage is tied to browser history. Some mobile browsers have an option to erase browser history on exit. If that box has a checkmark in it, then localStorage gets purged along with browser history everytime the browser is closed! So, you can ask the user to go into Settings and uncheck that box. Note: localStorage is not supported by older IE browsers, Opera Mini, and some Blackberry devices.

If cookies are enabled, you could use a one-time cookie code to log the user in automatically, but that cookie must expire after first use. Once the user is logged in, the server must issue a new cookie code to the client. And the client can use that new cookie to log in once again or keep alive the session. Using the same cookie twice should not work for security reasons. And the server must make sure never to issue the same code to two different users!

I remember, about 10 yrs ago, I went online to check my emails, and immediately the site had me logged into another user's account! I could have read that person's private emails and stuff, but I decided not to. I reported the incident to the admin. The problem was probably their server issued a random quick-login access code to me, and another user somehow got the same access code that I got. And when I opened the website, it thought that I was that other person. If your site deals with money and credit cards, you should avoid this technique! Any kind of auto login is a bad idea for a bank!

Upvotes: 1

gsthina
gsthina

Reputation: 1100

Save your data by using the following:

localStorage.setItem("variable", value);

Retrieve it from localstorage by using the following:

localStorage.getItem("variable");

It is as simple as it is. Reference here.

Upvotes: 0

Si-Mohamed
Si-Mohamed

Reputation: 11

There is a plug-in to store data in an SQLite database using Cordova in this link . You can find more details about storage with Cordova in the doc.

Upvotes: 0

Related Questions