I am making a mobile game with JQuery Mobile, a multipage template (so all pages in 1 html file, which makes it usable with PhoneGap).
Since it is HTML I am using JQuerys $.post function to send data to php scripts such as login.php, register.php, which add/update/delete data from the MySQL db.
When I $.post to login.php, upon authentication I return the users ID, then encrypt it client side using CryptoJS AES, store it in HTML5's localStorage, and when I need to make requests to protected pages, send this encrypted userID, decrypt it server side, and execute the MySQL commands assuming the user is authenticated.
The problem with this, is unlike cookies, localStorage variables can persist for long periods of time, which I believe would make my app potentially vulnerable to session hijacking if XSS could be executed.
I would like to keep things as secure as possible on behalf of my users, can someone tell me how to properly implement a safeguard against the session hijacking weakness of what I described?