Skip to content

Commit 792d53e

Browse files
authored
Merge pull request #35 from d-tree-org/31-questmalawi-core-android-app-remove-fetch-userinfo-step-on-login-steps
[Quest/Malawi Core] Android App : Remove fetch userinfo step on login steps
2 parents 836e40b + 63a6792 commit 792d53e

File tree

1 file changed

+9
-5
lines changed
  • android/engine/src/main/java/org/smartregister/fhircore/engine/ui/login

1 file changed

+9
-5
lines changed

android/engine/src/main/java/org/smartregister/fhircore/engine/ui/login/LoginViewModel.kt

+9-5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import androidx.lifecycle.MutableLiveData
2222
import androidx.lifecycle.ViewModel
2323
import androidx.lifecycle.viewModelScope
2424
import dagger.hilt.android.lifecycle.HiltViewModel
25+
import io.jsonwebtoken.Jwts
2526
import java.net.UnknownHostException
2627
import javax.inject.Inject
2728
import kotlinx.coroutines.CoroutineScope
@@ -126,9 +127,9 @@ constructor(
126127
_loginErrorState.postValue(LoginErrorState.ERROR_FETCHING_USER)
127128
}
128129

129-
private suspend fun fetchPractitioner(userInfo: UserInfo?): Result<Bundle> {
130+
private suspend fun fetchPractitioner(keycloakUuid: String?): Result<Bundle> {
130131
val endpointResult =
131-
userInfo?.keycloakUuid?.takeIf { it.isNotBlank() }?.practitionerEndpointUrl()?.runCatching {
132+
keycloakUuid?.takeIf { it.isNotBlank() }?.practitionerEndpointUrl()?.runCatching {
132133
fhirResourceService.getResource(url = this)
133134
}
134135
?: Result.failure(NullPointerException("Keycloak user is null. Failed to fetch user."))
@@ -194,10 +195,13 @@ constructor(
194195
val accessTokenResult = fetchAccessToken(trimmedUsername, passwordAsCharArray)
195196
if (accessTokenResult.isFailure) return
196197

197-
val userInfoResult = fetchUserInfo()
198-
if (userInfoResult.isFailure) return
198+
if (accessTokenResult.getOrNull() == null) return
199199

200-
val practitionerDetailsResult = fetchPractitioner(userInfoResult.getOrNull())
200+
val jwtParser = Jwts.parser()
201+
val jwt = accessTokenResult.getOrNull()!!.accessToken!!.substringBeforeLast('.').plus(".")
202+
val subClaim = jwtParser.parseClaimsJwt(jwt)
203+
val keycloakUuid = subClaim.body["sub"].toString()
204+
val practitionerDetailsResult = fetchPractitioner(keycloakUuid)
201205
if (practitionerDetailsResult.isFailure) return
202206

203207
savePractitionerDetails(practitionerDetailsResult.getOrDefault(Bundle()))

0 commit comments

Comments
 (0)