3

When I login to my PhoneGap app everything works correctly. I can make requests in the current session and I am authenticated. If I completely close the app and restart it though my session is gone. The cookie with the connect.sid does not persist between app restarts -- this is a PhoneGap issue I can't get around.

How should I get the connect.sid from the cookie?

How should I store it in localStorage?

How can I add the connect.sid back to the cookie on app start?

rgettman
  • 176,041
  • 30
  • 275
  • 357
boom
  • 10,856
  • 9
  • 43
  • 64
  • In IOS 7, cookies not stored( defaul). You must login to server when start app. Else, you must enable Cookies on IOS [see](http://stackoverflow.com/questions/3709315/phonegap-cookie-based-authentication-php-not-working-webview?rq=1) – Hanh Le Jan 08 '14 at 01:29
  • Be aware that localStorage values *are not* sent to the server. If you decided to use that, you'll still need to figure out how pass them to the server (or cookie them on app restart so that they are passed automatically to the server) – Hector Correa Jan 22 '14 at 02:09
  • @HectorCorrea This is exactly what I'm trying to figure out. Using localStorage to keep this data but then still sending it to the server. – boom Jan 22 '14 at 08:38
  • @boom Did you ever get this figured out? I'm wresting with the same exact problem! – tonejac May 06 '15 at 05:06

4 Answers4

1

I use LocalStorage and it persists, but if the phone requires more storage it can purge your apps browser cache, including cookies, LocalStorage and /tmp files.

Pete
  • 4,542
  • 9
  • 43
  • 76
-1

assuming you have your session id already in the variable session_id. You simply use the setItem and getItem methods to interact with the local storage.

to set a variable (note: the variable must be a string as localstorage only can handle this type. If you want to store an object or array you need to transform it into json with JSON.stringify())

window.localStorage.setItem("sessionid", session_id);

to get your sessionid from local storage

var id= window.localStorage.getItem("sessionid");
kasper Taeymans
  • 6,950
  • 5
  • 32
  • 51
  • Thanks but I'm not looking for how to use localStorage. I'm looking for how to pull the session id from the cookie and then how to correctly send it to the server when the app is resumed. – boom Jan 22 '14 at 08:39
  • why the downvote? I answered the question just like stated... "How can I store/retrieve the session_id in localStorage instead of a cookie? localStorage persists between app restarts" – kasper Taeymans Jan 23 '14 at 00:40
  • 1
    You answered the question, how can I store anything in localStorage. – boom Jan 23 '14 at 03:35
  • @boom: I know... :-) sometimes you receive downvotes for a correct answer. – kasper Taeymans Jan 23 '14 at 09:16
  • Correct answer for a totally different question maybe. You could edit it to answer the question I asked if you want to. – boom Jan 25 '14 at 06:48
  • Well, changing/editing the question after answers are submitted isn't nice. – kasper Taeymans Jan 27 '14 at 18:21
-1

Save session_id in local storage is not a better way. i will be cleared when your server is restarting. You need to save the session data in database. here is the example for mongodb. there is no additional coding for it, just pass the your database connection string in session declaration.

var MongoStore = require('connect-mongo')(express);
app.use(express.session({
    secret : "", 
    store : new MongoStore({ 
        db : "database_name",
        host : "database_host", 
        username : "database_username", 
        password : database_password, 
        auto_reconnect : true 
    })
}));
Sudhir Vadodariya
  • 391
  • 1
  • 2
  • 11
  • This does not work, this is not the problem. The session data and id is already available to the server the whole time. It's just when the phonegap app is restarted to client-side connect.sid is lost. – boom Jan 25 '14 at 06:46
-1

You could use the following procedure:

  • read the cookie using plain javascript or jquery-cookie, then save it to a persistent storage (e.g localstorage) on each request (allows you to maintain session freshness)
  • in deviceready handler read the session cookie from the persistent storage, then set it on the document, which is the reverse of the previous bullet (allows you to handle application restarts)
Vlad Stirbu
  • 1,732
  • 2
  • 16
  • 27