Adding tokens to our API calls
Because we structured the bases of our API calls in one file, we only must go to one file and change one line in each of our call functions. For our example we can look at the postCall
function with the code below:
// ingress/frontend/src/api/utils.ts
export async function postCall<T, X>(
url: string, body: T, expectedResponse: number) {
. . .
{
headers: {
'Content-Type': 'application/json',
'token': localStorage.getItem('token')
},
validateStatus: () => true
});
return handleRequest(response, expectedResponse);
}
Here we can see that we have changed the token
in the header from a string to localStorage.getItem('token')
. This means that we can get the auth token from the local storage of the browser and insert it into our header of the API call. We can make this change with the following functions in...