Login Repo
This commit is contained in:
@@ -183,4 +183,44 @@ class AuthRemoteDataProvider {
|
||||
return DC.error(AuthFailure.serverError(e));
|
||||
}
|
||||
}
|
||||
|
||||
Future<DC<AuthFailure, LoginDto>> login({
|
||||
required String phoneNumber,
|
||||
required String password,
|
||||
}) async {
|
||||
try {
|
||||
final response = await _apiClient.post(
|
||||
ApiPath.login,
|
||||
data: {'phone_number': phoneNumber, 'password': password},
|
||||
);
|
||||
|
||||
if (response.data['success'] == false) {
|
||||
if ((response.data['errors'] as List).isNotEmpty) {
|
||||
if (response.data['errors'][0]['code'] == "900") {
|
||||
return DC.error(
|
||||
AuthFailure.dynamicErrorMessage('Kamu Belum Terdaftar'),
|
||||
);
|
||||
} else {
|
||||
return DC.error(
|
||||
AuthFailure.dynamicErrorMessage(
|
||||
'Terjadi kesalahan coba lagi nanti',
|
||||
),
|
||||
);
|
||||
}
|
||||
} else {
|
||||
return DC.error(
|
||||
AuthFailure.dynamicErrorMessage(
|
||||
'Terjadi kesalahan coba lagi nanti',
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final dto = LoginDto.fromJson(response.data['data']);
|
||||
return DC.data(dto);
|
||||
} on ApiFailure catch (e, s) {
|
||||
log('login', name: _logName, error: e, stackTrace: s);
|
||||
return DC.error(AuthFailure.serverError(e));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -111,4 +111,28 @@ class AuthRepository implements IAuthRepository {
|
||||
return left(const AuthFailure.unexpectedError());
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Either<AuthFailure, Login>> login({
|
||||
required String phoneNumber,
|
||||
required String password,
|
||||
}) async {
|
||||
try {
|
||||
final result = await _remoteDataProvider.login(
|
||||
phoneNumber: phoneNumber,
|
||||
password: password,
|
||||
);
|
||||
|
||||
if (result.hasError) {
|
||||
return left(result.error!);
|
||||
}
|
||||
|
||||
final auth = result.data!.toDomain();
|
||||
|
||||
return right(auth);
|
||||
} catch (e, s) {
|
||||
log('loginError', name: _logName, error: e, stackTrace: s);
|
||||
return left(const AuthFailure.unexpectedError());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user