Problem
Authorization is one of the first problems developers face upon starting a new project. And one of the most common types of authorization (from my experience) is the token-based authorization (usually using JWT).
From my perspective, this article looks like "what I wanted to read two weeks ago". My goal was to write minimalistic and reusable code with a clean and straightforward interface. I had the next requirements for my implementation of the auth management:
- Tokens should be stored in local storage
- Tokens should be restored on page reload
- Access token should be passed in the network requests
- After expiration access token should be updated by refresh token if the last one is presented
- React components should have access to the auth information to render appropriate UI
- The solution should be made with pure React (without Redux, thunk, etc..)