Persisting JWTs to localStorage

To view this video please enable JavaScript, and consider upgrading to a web browser that supports HTML5 video

PRO

Use HTML5 localStorage to persist JWT between browsing sessions for authentication.

Weyou!

Help spread the word about this tutorial!

As long as the client possess a valid token, they can be considered
"authenticated." We can persist this state across multiple page visits by
storing the JWT using localStorage.

HTML5 localStorage is a key-value store that can be accessed on the window object. The data is stored separately from cookies and sessionStorage, and can persist for multiple browsing sessions until it is cleared by your app or the browser. All of the keys and values in localStorage are always strings.

Add a method to save the token to localStorage thereby logging the user in: