1111
This commit is contained in:
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user