2025-11-28 10:14:30 +05:30
|
|
|
import 'package:dio/dio.dart';
|
2025-12-03 11:57:05 +05:30
|
|
|
import 'package:flutter/material.dart';
|
2025-11-28 10:14:30 +05:30
|
|
|
import '../config/api_config.dart';
|
2025-12-03 11:57:05 +05:30
|
|
|
import 'dio_client.dart';
|
2025-11-28 10:14:30 +05:30
|
|
|
|
|
|
|
|
class AuthService {
|
2025-12-03 11:57:05 +05:30
|
|
|
late final Dio _dio;
|
2025-11-28 10:14:30 +05:30
|
|
|
|
2025-12-03 11:57:05 +05:30
|
|
|
AuthService(BuildContext context) {
|
|
|
|
|
_dio = DioClient.getInstance(context);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Login API
|
2025-11-28 10:14:30 +05:30
|
|
|
Future<Map<String, dynamic>> login(String loginId, String password) async {
|
|
|
|
|
try {
|
|
|
|
|
final response = await _dio.post('/user/login', data: {
|
|
|
|
|
'login_id': loginId,
|
|
|
|
|
'password': password,
|
|
|
|
|
});
|
|
|
|
|
|
2025-12-03 11:57:05 +05:30
|
|
|
return Map<String, dynamic>.from(response.data);
|
2025-11-28 10:14:30 +05:30
|
|
|
} on DioException catch (e) {
|
2025-12-03 11:57:05 +05:30
|
|
|
final data = e.response?.data;
|
2025-11-28 10:14:30 +05:30
|
|
|
return {
|
|
|
|
|
'success': false,
|
2025-12-03 11:57:05 +05:30
|
|
|
'message': data is Map && data['message'] != null
|
|
|
|
|
? data['message']
|
|
|
|
|
: e.message ?? 'Login failed'
|
2025-11-28 10:14:30 +05:30
|
|
|
};
|
|
|
|
|
} catch (e) {
|
2025-12-03 11:57:05 +05:30
|
|
|
return {'success': false, 'message': e.toString()};
|
2025-11-28 10:14:30 +05:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-03 11:57:05 +05:30
|
|
|
/// Logout API
|
|
|
|
|
Future<Map<String, dynamic>> logout() async {
|
2025-11-28 10:14:30 +05:30
|
|
|
try {
|
2025-12-03 11:57:05 +05:30
|
|
|
final response = await _dio.post('/user/logout');
|
|
|
|
|
return Map<String, dynamic>.from(response.data);
|
2025-11-28 10:14:30 +05:30
|
|
|
} catch (e) {
|
|
|
|
|
return {'success': false, 'message': e.toString()};
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-12-03 11:57:05 +05:30
|
|
|
|
|
|
|
|
/// Refresh token
|
|
|
|
|
Future<Map<String, dynamic>> refreshToken(String oldToken) async {
|
|
|
|
|
try {
|
|
|
|
|
final response = await _dio.post(
|
|
|
|
|
'/user/refresh',
|
|
|
|
|
options: Options(headers: {
|
|
|
|
|
'Authorization': 'Bearer $oldToken',
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return Map<String, dynamic>.from(response.data);
|
|
|
|
|
} on DioException catch (e) {
|
|
|
|
|
final msg = e.response?.data?['message'] ?? 'Refresh failed';
|
|
|
|
|
return {'success': false, 'message': msg};
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-11-28 10:14:30 +05:30
|
|
|
}
|