This commit is contained in:
Mohammed Al-Samarraie
2026-01-13 13:07:31 +03:00
parent ac8a769ff0
commit fa4bee4771
15 changed files with 578 additions and 45 deletions

View File

@@ -1,10 +1,13 @@
import 'package:dio/dio.dart';
import 'package:shared_preferences/shared_preferences.dart';
class ApiClient {
final Dio dio;
static const String baseUrl = 'YOUR_API_BASE_URL_HERE';
final SharedPreferences? sharedPreferences;
static const String baseUrl = 'https://hrm.go.iq/api';
static const String _tokenKey = 'user_token';
ApiClient({required this.dio}) {
ApiClient({required this.dio, this.sharedPreferences}) {
dio.options = BaseOptions(
baseUrl: baseUrl,
connectTimeout: const Duration(seconds: 30),
@@ -15,6 +18,20 @@ class ApiClient {
},
);
// Add interceptor to add token to requests
dio.interceptors.add(
InterceptorsWrapper(
onRequest: (options, handler) async {
// Get token from SharedPreferences
final token = sharedPreferences?.getString(_tokenKey);
if (token != null && token.isNotEmpty) {
options.headers['Authorization'] = 'Bearer $token';
}
return handler.next(options);
},
),
);
// Add interceptors for logging and error handling
dio.interceptors.add(
LogInterceptor(