2025-08-02 23:10:48 +07:00
|
|
|
import 'dart:developer';
|
|
|
|
|
|
2025-07-30 22:38:44 +07:00
|
|
|
import 'package:dartz/dartz.dart';
|
2025-08-02 23:10:48 +07:00
|
|
|
import 'package:dio/dio.dart';
|
2025-07-30 22:38:44 +07:00
|
|
|
import 'package:enaklo_pos/core/constants/variables.dart';
|
2025-08-02 23:10:48 +07:00
|
|
|
import 'package:enaklo_pos/core/network/dio_client.dart';
|
2025-07-30 22:38:44 +07:00
|
|
|
import 'package:enaklo_pos/data/datasources/auth_local_datasource.dart';
|
|
|
|
|
import 'package:enaklo_pos/data/models/response/auth_response_model.dart';
|
|
|
|
|
|
|
|
|
|
class AuthRemoteDatasource {
|
2025-08-02 23:10:48 +07:00
|
|
|
final Dio dio = DioClient.instance;
|
2025-07-30 22:38:44 +07:00
|
|
|
Future<Either<String, AuthResponseModel>> login(
|
|
|
|
|
String email, String password) async {
|
2025-08-02 23:10:48 +07:00
|
|
|
final url = '${Variables.baseUrl}/api/v1/auth/login';
|
|
|
|
|
log(url);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
final response = await dio.post(
|
|
|
|
|
url,
|
|
|
|
|
data: {
|
|
|
|
|
'email': email,
|
|
|
|
|
'password': password,
|
|
|
|
|
},
|
|
|
|
|
);
|
2025-07-30 22:38:44 +07:00
|
|
|
|
2025-08-02 23:10:48 +07:00
|
|
|
if (response.statusCode == 200) {
|
|
|
|
|
return Right(AuthResponseModel.fromMap(response.data));
|
|
|
|
|
} else {
|
|
|
|
|
return const Left('Failed to login');
|
|
|
|
|
}
|
|
|
|
|
} on DioException catch (e) {
|
|
|
|
|
log("Dio error: ${e.message}");
|
|
|
|
|
return Left(e.response?.data['message'] ?? 'Login gagal');
|
|
|
|
|
} catch (e) {
|
|
|
|
|
log("Unexpected error: $e");
|
|
|
|
|
return const Left('Unexpected error occurred');
|
2025-07-30 22:38:44 +07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//logout
|
|
|
|
|
Future<Either<String, bool>> logout() async {
|
2025-08-02 23:10:48 +07:00
|
|
|
try {
|
|
|
|
|
final authData = await AuthLocalDataSource().getAuthData();
|
|
|
|
|
final url = '${Variables.baseUrl}/api/v1/auth/logout';
|
|
|
|
|
|
|
|
|
|
final response = await dio.post(
|
|
|
|
|
url,
|
|
|
|
|
options: Options(
|
|
|
|
|
headers: {
|
|
|
|
|
'Authorization': 'Bearer ${authData.token}',
|
|
|
|
|
'Accept': 'application/json',
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
);
|
2025-07-30 22:38:44 +07:00
|
|
|
|
2025-08-02 23:10:48 +07:00
|
|
|
if (response.statusCode == 200) {
|
|
|
|
|
return const Right(true);
|
|
|
|
|
} else {
|
|
|
|
|
return const Left('Failed to logout');
|
|
|
|
|
}
|
|
|
|
|
} on DioException catch (e) {
|
|
|
|
|
return Left(e.response?.data['message'] ?? 'Logout gagal');
|
|
|
|
|
} catch (e) {
|
|
|
|
|
return const Left('Unexpected error occurred');
|
2025-07-30 22:38:44 +07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|