Skip to content

Commit

Permalink
fix: Login screen crashes when we fill wrong login details (#2288)
Browse files Browse the repository at this point in the history
* fix: Login screen crashes when we fill wrong login details MIFOSAC-315

* fixed spotless
  • Loading branch information
itsPronay authored Jan 26, 2025
1 parent 876e30f commit 2c3d991
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -25,12 +26,10 @@ class LoginUseCase(private val loginRepository: LoginRepository) {
username: String,
password: String,
): Flow<Resource<PostAuthenticationResponse>> = 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()))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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() {

Expand Down Expand Up @@ -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)
}
}
}
}
Expand Down

0 comments on commit 2c3d991

Please sign in to comment.