Fetch Protected Data from a Resource Server with an Access Token

Share this video with your friends

Social Share Links

Send Tweet
Published 3 years ago
Updated 3 years ago

We're going to fetch a user with that token we exchanged the code parameter for in the previous lesson.

All we have to do is make a fetch request to the /user endpoint with an Authorization header that contains the token and then return the data. Like this Authorization: `Bearer ${token}

Instructor: [0:00] Replace the console.log with a call to fetchUser function, which takes the token and returns a list of users and we can respond with this list. Create the fetchUser function and start with a variable that stores the URL to the API endpoint for fetching user data from Github.

[0:28] Now use fetch to make a request to the endpoint, passing it to the URL. Set the headers to an object that contains the authorization header. The value of the authorization header should be prefixed with bearer, and actual value should be the token.

[0:47] Get to JSON data from the response and return the data. Hit to the browser, and before clicking the Authorize link, open the terminal. Click on Settings and make sure Preserve Log is checked. If we don't, we won't catch the printed user between redirects.

[1:08] Now, click the Authorize link, and even though the site redirects to the Index page, you still get the user payload in the console.

egghead
egghead
~ just now

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today