Skip to content

Commit 2c3d991

Browse files
authored
fix: Login screen crashes when we fill wrong login details (#2288)
* fix: Login screen crashes when we fill wrong login details MIFOSAC-315 * fixed spotless
1 parent 876e30f commit 2c3d991

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

core/domain/src/main/java/com/mifos/core/domain/useCases/LoginUseCase.kt

+6-7
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ package com.mifos.core.domain.useCases
1212
import com.mifos.core.common.utils.Resource
1313
import com.mifos.core.data.repository.LoginRepository
1414
import kotlinx.coroutines.flow.Flow
15+
import kotlinx.coroutines.flow.catch
1516
import kotlinx.coroutines.flow.flow
1617
import org.openapitools.client.models.PostAuthenticationResponse
1718

@@ -25,12 +26,10 @@ class LoginUseCase(private val loginRepository: LoginRepository) {
2526
username: String,
2627
password: String,
2728
): Flow<Resource<PostAuthenticationResponse>> = flow {
28-
try {
29-
emit(Resource.Loading())
30-
val result = loginRepository.login(username, password)
31-
emit(Resource.Success(result))
32-
} catch (e: Exception) {
33-
emit(Resource.Error(e.message.toString()))
34-
}
29+
emit(Resource.Loading())
30+
val result = loginRepository.login(username, password)
31+
emit(Resource.Success(result))
32+
}.catch { e ->
33+
emit(Resource.Error(e.message.toString()))
3534
}
3635
}

feature/auth/src/main/java/com/mifos/feature/auth/login/LoginViewModel.kt

+8-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import androidx.lifecycle.viewModelScope
1616
import com.mifos.core.common.utils.Network
1717
import com.mifos.core.common.utils.Resource
1818
import com.mifos.core.datastore.PrefManager
19+
import com.mifos.core.domain.useCases.LoginUseCase
1920
import com.mifos.core.domain.useCases.PasswordValidationUseCase
2021
import com.mifos.core.domain.useCases.UsernameValidationUseCase
2122
import com.mifos.core.model.getInstanceUrl
@@ -41,7 +42,7 @@ class LoginViewModel @Inject constructor(
4142
private val usernameValidationUseCase: UsernameValidationUseCase,
4243
private val passwordValidationUseCase: PasswordValidationUseCase,
4344
private val baseApiManager: BaseApiManager,
44-
private val loginUseCase: com.mifos.core.domain.useCases.LoginUseCase,
45+
private val loginUseCase: LoginUseCase,
4546
) :
4647
ViewModel() {
4748

@@ -99,7 +100,12 @@ class LoginViewModel @Inject constructor(
99100
}
100101

101102
is Resource.Success -> {
102-
result.data?.let { onLoginSuccessful(it, username, password) }
103+
if (result.data?.authenticated == true && result.data != null) {
104+
onLoginSuccessful(result.data!!, username, password)
105+
} else {
106+
_loginUiState.value =
107+
LoginUiState.ShowError(R.string.feature_auth_error_login_failed)
108+
}
103109
}
104110
}
105111
}

0 commit comments

Comments
 (0)