HTTP interceptor
What is an HTTP interceptor? An HTTP interceptor is a piece of functionality that intercepts incoming HTTP responses and outgoing HTTP requests. We can automatically change or modify the header of our requests before sending them to the endpoints of web services. Luckily, Axios has HTTP interceptor interfaces readily available for developers to use. So, let's get started!
The first thing we must do is update the auth.service.js
file of the src/auth
folder with the following code:
const key = "token";
The token's key name is simply key
.
We'll also be adding two functions to the auth.service.js
file:
export function getToken() { Â Â return localStorage.getItem(key); } export function logOut() { Â Â localStorage.clear(); Â Â window.location = "/login"; }
The getToken
function gets the token's value from the local storage, while the logout
function clears out any stored values in the local...