Using fetch to interact with authenticated REST API endpoints
In this section, we'll properly wire up posting questions and answers to our REST API. As part of this work, we will enhance our http
function to use a bearer token from Auth0 in the HTTP request. This is because the endpoints for posting questions and answers are protected in the REST API and require a valid bearer token.
All of our changes will be in QuestionsData.ts
—our user interface components will be unchanged.
Posting a question to the REST API
We are going to change the implementation for posting a question to use an access token from Auth0:
- Let's start by importing the function that gets the access token from Auth0 into
QuestionsData.ts
:import { getAccessToken } from './Auth';
- Let's change the implementation of the
postQuestion
function to the following:export const postQuestion...