Courses

[NEW] Flutter 3 Mobile App with Laravel 12 API

Handling Token Expiration

While doing our final application testing, we noticed that the token expiration was not handled properly. When the token expires, the application crashes. We need to handle this situation gracefully by logging the user out and redirecting them to the login page.

So, let's handle all of the 401 responses from the server and log the user out if the token has expired.


Adding AuthProvider to Api Service

First, we have to modify our API Service to accept AuthProvider. This will allow us to log the user out when the token expires:

import 'package:laravel_api_flutter_app/providers/auth_provider.dart';
 
// ...
 
class ApiService {
late String token;
late AuthProvider? authProvider;
 
ApiService(String token) {
ApiService(String token, AuthProvider? auth) {
token = token;
authProvider = auth;
}
 
// ...

Now, we need to pass that AuthProvider...

The full lesson is only for Premium Members.
Want to access all 26 lessons of this course? (115 min read)

You also get:

  • 75 courses
  • Premium tutorials
  • Access to repositories
  • Private Discord