diff --git a/core/domain/src/main/java/com/mifos/core/domain/useCases/LoginUseCase.kt b/core/domain/src/main/java/com/mifos/core/domain/useCases/LoginUseCase.kt index 8d6ea671ef..391a5033c9 100644 --- a/core/domain/src/main/java/com/mifos/core/domain/useCases/LoginUseCase.kt +++ b/core/domain/src/main/java/com/mifos/core/domain/useCases/LoginUseCase.kt @@ -12,6 +12,7 @@ package com.mifos.core.domain.useCases import com.mifos.core.common.utils.Resource import com.mifos.core.data.repository.LoginRepository import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.catch import kotlinx.coroutines.flow.flow import org.openapitools.client.models.PostAuthenticationResponse @@ -25,12 +26,10 @@ class LoginUseCase(private val loginRepository: LoginRepository) { username: String, password: String, ): Flow> = flow { - try { - emit(Resource.Loading()) - val result = loginRepository.login(username, password) - emit(Resource.Success(result)) - } catch (e: Exception) { - emit(Resource.Error(e.message.toString())) - } + emit(Resource.Loading()) + val result = loginRepository.login(username, password) + emit(Resource.Success(result)) + }.catch { e -> + emit(Resource.Error(e.message.toString())) } } diff --git a/feature/auth/src/main/java/com/mifos/feature/auth/login/LoginViewModel.kt b/feature/auth/src/main/java/com/mifos/feature/auth/login/LoginViewModel.kt index 6e6fa2f078..2c2f1e3232 100644 --- a/feature/auth/src/main/java/com/mifos/feature/auth/login/LoginViewModel.kt +++ b/feature/auth/src/main/java/com/mifos/feature/auth/login/LoginViewModel.kt @@ -16,6 +16,7 @@ import androidx.lifecycle.viewModelScope import com.mifos.core.common.utils.Network import com.mifos.core.common.utils.Resource import com.mifos.core.datastore.PrefManager +import com.mifos.core.domain.useCases.LoginUseCase import com.mifos.core.domain.useCases.PasswordValidationUseCase import com.mifos.core.domain.useCases.UsernameValidationUseCase import com.mifos.core.model.getInstanceUrl @@ -41,7 +42,7 @@ class LoginViewModel @Inject constructor( private val usernameValidationUseCase: UsernameValidationUseCase, private val passwordValidationUseCase: PasswordValidationUseCase, private val baseApiManager: BaseApiManager, - private val loginUseCase: com.mifos.core.domain.useCases.LoginUseCase, + private val loginUseCase: LoginUseCase, ) : ViewModel() { @@ -99,7 +100,12 @@ class LoginViewModel @Inject constructor( } is Resource.Success -> { - result.data?.let { onLoginSuccessful(it, username, password) } + if (result.data?.authenticated == true && result.data != null) { + onLoginSuccessful(result.data!!, username, password) + } else { + _loginUiState.value = + LoginUiState.ShowError(R.string.feature_auth_error_login_failed) + } } } }