I have been researching how best to store authentication tokens in a Single Page Application (SPA). There is some existing debate about this topic on SO but as far as I can see, none offer concrete solutions.
Having spent much of yesterday and today trawling the internet for answers, I came across the following:
Local Storage API. I found that some basic guides suggest the use of
localStorage
(though many rightfully advise against it). I do not like this approach because data stored inlocalStorage
could be accessed in the event of an XSS attack.Web Workers. If the token is stored in a web worker, the user will not be logged in if a new tab is opened. This makes for a substandard and confusing user experience.
Closures. Same as Web Workers - there is no persistence.
HttpOnly Cookies. On the one hand, I read that this can protect from XSS. However, on the other hand, wouldn't this mean that we now have to deal with CSRF? Then it's a new debate altogether: how does one implement CSRF tokens with an SPA + REST API?
How is everyone else doing it?