I\'m familiar with Web Storage APIs and cookies but I can\'t figure what is the most secure way to store an authentication token. I\'m wondering if this might break any thir
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.
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
and sessionStorage
both extend Storage
. 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.
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.Referer
and Origin
header.Original article: https://auth0.com/docs/security/store-tokens#how-to-implement
Checkout this for motivation
The most secure option is in-memory. Checkout this for a deep dive