This repository demonstrates a complete working example of handling access token refreshing in a client-server architecture using:
- Ktor as the backend server
- Flutter as the mobile client app
- π§Ύ JWT-based authentication with access and refresh tokens
- β± Access token expires in 1 minute (for testing)
- β»οΈ Refresh token expires in 2 minutes
- π Automatic access token refresh when expired
- πͺ Forced logout when refresh token is also expired
β Server (Ktor)
-
Exposes endpoints for:
- /auth/signup
- /auth/login
- /auth/refresh
- /get_data?userId
-
Generates JWTs using HMAC
-
Stores refresh tokens and validates them during token refresh
π± Client (Flutter)
-
Stores tokens securely using
flutter_secure_storage
-
Intercepts HTTP 401 errors
- Automatically retries the original request after refreshing the token
- If refresh token is also expired, logs the user out
-
Uses state management BLoC to manage state
- Start the Ktor Server
cd ktor-server
./gradlew run
- Run the Flutter App
cd flutter-client
flutter run