1

I have an app in phonegap (pure js, html, css) where user signs in and then it has access to some aditional sites. My question is how to save his personal data or his session. There is no php involved for sending cookie or handling sessions. Everything need to be saved in JS (jQuery). How can I do this?

2 Answers 2

1

You can use HTML5 feature Local Storage

For eg;

When user logs in, set one variable as isAuthenticated in your local storage

By default, it will be 0, on successful login, set it to 1.

localStorage.isAuthenticated = 0; // default
OR
localStorage.setItem('isAuthenticated',0);

On Successful login

localStorage.isAuthenticated = 1; // default
OR
localStorage.setItem('isAuthenticated',1);

And whenever you want to check user's authenticity, simply compare value of isAuthenticated variable from Local Storage.

var isLoggedIn = localStorage.getItem('isAuthenticated');

if(isLoggedIn){
    //your code
} else {
    // invalid user code
}

UPDATE If you are storing javascript objects then

var myObject = { 'one': 1, 'two': 2, 'three': 3 };

// Put the object into storage
localStorage.setItem('myObject', JSON.stringify(myObject));

// Retrieve the object from storage
var retrievedObject = localStorage.getItem('myObject');
retrievedObject = JSON.parse(retrievedObject)

Convert your object into string using JSON.stringify and then store it into the local storage. While retrieving cast that string back into the javascript object using JSON.parse

Looking at the local storage documentation, the functionality seems to be limited to handle only string key/value pairs.

You can take a look at the documentation here

Apple

Mozilla

PS : Phonegap has nothing to do with working of local storage or any other HTML feature. Phonegap is just a middleware that facilitates communication between your device hardware and your javascript/html code.

Sign up to request clarification or add additional context in comments.

3 Comments

I already saw that solution but it doesnt work with phonegap. Dont know really why
It works... I am using the same thing with phonegap... Just a doubt, are you storing javascript objects?
haha :).. No it doesn't... You can always get a workaround for that though... Check the updated answer :)
0

Here's the PhoneGap documentation on LocalStorage and SQL Database:

http://docs.phonegap.com/en/2.0.0/cordova_storage_storage.md.html#localStorage

1 Comment

But if I write in local storage it will stay there when the user close the app (well I'll have to erase it by myself, but I would like to be erased automaticly)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.