I've heard about browser storage and cookies but can't figure what is the best secure way to store a token. Also don't know if other methods exists, or if any third-part libraries does the work correctly.
I'd like to have an exhaustive list of available methods to do so, with advantages/inconvenients of each and the best way above all, if any.
Checkout this for motivation
The most secure option is in-memory. Checkout this for a deep dive
Where to Store Your JWTs
With token-based authentication, you are given the choice of where to store the JWT. We strongly recommend that you store your tokens in local storage/session storage or a cookie.
Web Storage (local storage/session storage)
Commonly, the JWT is placed in the browsers local storage and this works well for most use cases.
When logging in a user with a username and password, the response body contains the
access_token JWT
. Then you need to handle this response in the client side code. This token can then be stored in localStorage or sessionStorage.Click here for an example using sessionStorage
Both
localStorage
andsessionStorage
both extendStorage
. The only difference between them is the persistance of the data:localStorage
- data persists until explicitly deleted. Changes made are saved and available for all current and future visits to the site.sessionStorage
- Changes made are saved and available for the current page, as well as future visits to the site on the same window. Once the window is closed, the storage is deleted.Web Storage Disadvantages
Using Cookies
You can also use cookies to store the JWT. The exact way to set a cookie depends on the client side language you are using.
There are different options to control the lifetime of a cookie:
httpOnly
flag is set.Cookie Disadvantages
Referer
andOrigin
header.Original article: https://auth0.com/docs/security/store-tokens#how-to-implement