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...
Hello,
Could you eventually make an example of token expiration for refreshing the token and keeping the login alive while we're still in the application ?
Hello, well take a look into this :)
ps. With sanctum you can set them to never expire.