Writing the Requests Service
Explore how to build a requests service using Axios in React to handle authentication flows, including access token management, automatic token refresh with axios-auth-refresh, and error handling for protected routes. This lesson equips you to efficiently perform CRUD operations and manage secure API interactions in full stack applications.
We'll cover the following...
Now that we understand the authentication flow from the frontend side, let’s write the requests service we will use for data fetching and performing CRUD actions.
Making requests in JavaScript is relatively easy. The node environment and the browser provide native packages such as fetch to allow you to request a server. However, this project will use the axios package for HTTP requests.
Axios Installation
Axios is a popular library mainly used to send asynchronous HTTP requests to REST endpoints. Axios is the perfect library for CRUD operations. However, we will also install axios-auth-refresh. This simple library assists with an automatic refresh of tokens via axios interceptors. To install the axios and axios-auth-refresh packages, we follow these steps: ...