A cache service using localStorage
We must be able to cache the authentication status of the logged-in user. As mentioned, otherwise, with every page refresh, the user must go through the login routine. We need to update AuthService
so that it persists the auth status.
There are three main ways to store data:
cookie
localStorage
sessionStorage
Cookies, while they have their use cases, should not be used to store secure data because they can be sniffed or stolen by bad actors. In addition, cookies can store only 4 KB of data and can be set to expire.
localStorage
and sessionStorage
are similar. They are protected and isolated browser-side stores that allow the storage of larger amounts of data for your application. Unlike cookies, you can’t set an expiration date-time on values stored in either store. Values stored in either store survive page reloads and restores, making them better candidates than cookies for caching information.