Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

Using API Authentication Tokens

Premium
5 min read

Now that our authentication is completed, we can work on consuming API tokens within our application. Tokens are important to limit access to our application.

In this lesson, we will:

  • Store the token in our Auth Provider
  • Add Headers to our API requests - including the token
  • Refactor our API Service to use the token
  • And some other minor changes

Let's start by refactoring our API service to use the token.


Refactoring API Service - Consuming API Tokens

Currently, we have no way to pass the token to our API service, so let's add that:

Note: We will declare the token as a [non-nullable variable] using the...

The Full Lesson is Only for Premium Members

Want to access all of our courses? (29 h 14 min)

You also get:

54 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

Comments & Discussion

LA
Luis Antonio Parrado ✓ Link copied!

I found some errors:

  • In lib/services/api.dart in the method Future saveCategory(Category category) the headers was added in the body.
  • We also can't find where the changes were made to the lib/providers/auth_provider.dart
M
Modestas ✓ Link copied!

I've updated the saveCategory method to be correct, thanks!

As for the changes - they did not impact the code at this point (for example, the removal of return has 0 impact at this current stage) :)

SM
Smart Med ✓ Link copied!

You did not mention changes in lib/providers/auth_provider.dart, I checked "Github Commits" to know what was the changes

M
Modestas ✓ Link copied!

Those changes are not that inportant if I'm being honest. These were precautionary changes, but it works without them (at least it worked when the tutorial was made). If it does not work - I can update the lesson!

MK
Matthew Kille ✓ Link copied!

The addition of AuthProvider token is important!

M
Modestas ✓ Link copied!

Oh! Terribly sorry, I completely missed that specific line and focused on the return types/returns.

Indeed then I missed it three times... Will update the lesson tomorrow.

M
Modestas ✓ Link copied!

Updated the lesson