ADD ALOT OF POLISHING AND UPDATES...
This commit is contained in:
@@ -12,19 +12,34 @@ class AuthRepositoryImpl implements AuthRepository {
|
||||
AuthRepositoryImpl(this.remoteDataSource, this.secureStorage);
|
||||
|
||||
@override
|
||||
Future<Either<Failure, UserEntity>> login(String email, String password) async {
|
||||
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: ', '')));
|
||||
return Left(ServerFailure(_safeAuthFailure(e)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
String _safeAuthFailure(Object error) {
|
||||
final message = error.toString().replaceFirst(RegExp(r'^\w+:\s*'), '').trim();
|
||||
final lower = message.toLowerCase();
|
||||
final technical = lower.contains('exception') ||
|
||||
lower.contains('typeerror') ||
|
||||
lower.contains('stacktrace') ||
|
||||
lower.contains('instance of') ||
|
||||
lower.contains('package:');
|
||||
if (message.isEmpty || technical) {
|
||||
return 'Login gagal. Periksa email dan password kamu.';
|
||||
}
|
||||
return message;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user