Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use html5 local storage for storing user authentication session information

QUICK BACKGROUND:

I'm writing a Mongo/Express/Angular/Node SPA and using passport I have setup OAuth log in options to handle the user authentication / authorization.

With passport I am successfully maintaining session on the server, so all my XHR requests (that need it) are checking for a logged in user.

On log in the server puts the basic user session info into a cookie for the client to find on the authorization callback, I then am using angular-cookies' $CookieStore to access that cookie on the client save it to the rootscope and clear the cookie.

PROBLEM:

This is working perfectly except for any event where the user refreshes the browser, which causes my rootscope session to obviously get wiped.

So I was considering storing session information in the browser local storage (using store.js) then even on the initial load I could check for the session existing in the browser local storage and bypass the OAuth login if there was already a session.

Is it bad practice or is there some logistical/security problems with storing user session information in the browser local storage?

This app doesn't have any senstive data, sign up is free and logging in is really only there so I can track users and store data created in the application for each user. And the user session would never have a password (I only allow OAuth login no local option).

like image 391
Collin Estes Avatar asked Jul 06 '13 16:07

Collin Estes


People also ask

Is it safe to use LocalStorage for authentication?

If a site is vulnerable to XSS, LocalStorage is not safe Local storage shares many of the same characteristics as a cookie, including the same security risks. One of those is susceptibility to cross-site scripting, which steals cookies to let hackers masquerade as a user with their login session for a site.

Can I store user ID in LocalStorage?

So, for storing user information like user ID and password, we will use LocalStorage and the currently logged-in user's info will be visible inside the SessionStorage itself!

Does HTML5 have session storage?

HTML5 introduces the sessionStorage attribute which would be used by the sites to add data to the session storage, and it will be accessible to any page from the same site opened in that window, i.e., session and as soon as you close the window, the session would be lost.

Is it good to store user data in LocalStorage?

Never store sensitive information in LocalStorage. Remember, every JavaScript file that is loaded on your domain has access to LocalStorage. If malicious JavaScript code is added by you or your dependencies, they can retrieve user data or tokens you use to authenticate with APIs.


1 Answers

Instead of the localStorage, look at sessionStorage object: http://www.w3schools.com/html/html5_webstorage.asp

It works exactly like localStorage, but the whole sessionStorage object will be deleted when the browser window is closed - but will survive any page refreshes. It is an ideal place for storing session ids and alike.

But be warned that the sessionStorage is isolated within a browser tab - that means if your user choses to open a link in a new tab, the sessionStorage for that will be initialized empty.

like image 99
Dynalon Avatar answered Oct 18 '22 06:10

Dynalon