first week launch
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
import '../domain/user_entity.dart';
|
||||
|
||||
class AuthModel extends UserEntity {
|
||||
AuthModel({required super.token, required super.role});
|
||||
|
||||
factory AuthModel.fromJson(Map<String, dynamic> json) {
|
||||
// Ngambil dari json['data'] karena API Spring Boot lu sekarang pake ApiResponse
|
||||
return AuthModel(
|
||||
token: json['data']['token'],
|
||||
role: json['data']['role'],
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import '../../../../core/api_service.dart';
|
||||
import 'auth_model.dart';
|
||||
|
||||
abstract class AuthRemoteDataSource {
|
||||
Future<AuthModel> login(String email, String password);
|
||||
}
|
||||
|
||||
class AuthRemoteDataSourceImpl implements AuthRemoteDataSource {
|
||||
final ApiService apiService;
|
||||
|
||||
AuthRemoteDataSourceImpl(this.apiService);
|
||||
|
||||
@override
|
||||
Future<AuthModel> login(String email, String password) async {
|
||||
try {
|
||||
final response = await apiService.post('/auth/login', {
|
||||
'email': email,
|
||||
'password': password,
|
||||
});
|
||||
|
||||
// Karena API response lu ada "success": true
|
||||
if (response.statusCode == 200 && response.data['success'] == true) {
|
||||
return AuthModel.fromJson(response.data);
|
||||
} else {
|
||||
throw Exception(response.data['message'] ?? 'Gagal login');
|
||||
}
|
||||
} on DioException catch (e) {
|
||||
// Nangkep error dari backend (misal status 400/401)
|
||||
throw Exception(e.response?.data['message'] ?? 'Terjadi kesalahan jaringan');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
import 'package:dartz/dartz.dart';
|
||||
import '../../../../core/failure.dart';
|
||||
import '../../../../core/secure_storage.dart';
|
||||
import '../domain/auth_repository.dart';
|
||||
import '../domain/user_entity.dart';
|
||||
import 'auth_remote_data_source.dart';
|
||||
|
||||
class AuthRepositoryImpl implements AuthRepository {
|
||||
final AuthRemoteDataSource remoteDataSource;
|
||||
final SecureStorage secureStorage;
|
||||
|
||||
AuthRepositoryImpl(this.remoteDataSource, this.secureStorage);
|
||||
|
||||
@override
|
||||
Future<Either<Failure, UserEntity>> login(String email, String password) async {
|
||||
try {
|
||||
// 1. Suruh data source nembak API
|
||||
final userModel = await remoteDataSource.login(email, password);
|
||||
|
||||
// 2. Simpan token langsung ke HP biar UI gausah ribet
|
||||
await secureStorage.saveToken(userModel.token);
|
||||
|
||||
// 3. Balikin data sukses (Right)
|
||||
return Right(userModel);
|
||||
} catch (e) {
|
||||
// 4. Balikin pesan error (Left)
|
||||
return Left(ServerFailure(e.toString().replaceAll('Exception: ', '')));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
import 'package:dartz/dartz.dart';
|
||||
import '../../../../core/failure.dart';
|
||||
import 'user_entity.dart';
|
||||
|
||||
abstract class AuthRepository {
|
||||
// Pake Either: Kiri balikin Failure, Kanan balikin UserEntity
|
||||
Future<Either<Failure, UserEntity>> login(String email, String password);
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
class UserEntity {
|
||||
final String token;
|
||||
final String role;
|
||||
|
||||
UserEntity({required this.token, required this.role});
|
||||
}
|
||||
Reference in New Issue
Block a user