Skip to content

Commit

Permalink
Fix first time login error 'failed to fetch user details' (#3633)
Browse files Browse the repository at this point in the history
First time request to /userinfo failed to pick up on saved access token
  • Loading branch information
LZRS authored Nov 25, 2024
1 parent 234c571 commit 61412d8
Showing 1 changed file with 15 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ import java.util.Base64
import javax.inject.Inject
import javax.inject.Singleton
import javax.net.ssl.SSLHandshakeException
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import org.smartregister.fhircore.engine.configuration.app.ConfigService
import org.smartregister.fhircore.engine.data.remote.auth.OAuthService
import org.smartregister.fhircore.engine.data.remote.model.response.OAuthResponse
Expand Down Expand Up @@ -201,27 +200,27 @@ constructor(
}
}

private fun saveToken(
private suspend fun saveToken(
username: String,
password: CharArray,
oAuthResponse: OAuthResponse,
) {
accountManager.run {
val account =
accounts.find { it.name == username && it.type == authConfiguration.accountType }
if (account != null) {
setPassword(account, oAuthResponse.refreshToken)
setAuthToken(account, AUTH_TOKEN_TYPE, oAuthResponse.accessToken)
} else {
val newAccount = Account(username, authConfiguration.accountType)
addAccountExplicitly(newAccount, oAuthResponse.refreshToken, null)
setAuthToken(newAccount, AUTH_TOKEN_TYPE, oAuthResponse.accessToken)
withContext(dispatcherProvider.io()) {
with(accountManager) {
val account =
accounts.find { it.name == username && it.type == authConfiguration.accountType }
if (account != null) {
setPassword(account, oAuthResponse.refreshToken)
setAuthToken(account, AUTH_TOKEN_TYPE, oAuthResponse.accessToken)
} else {
val newAccount = Account(username, authConfiguration.accountType)
addAccountExplicitly(newAccount, oAuthResponse.refreshToken, null)
setAuthToken(newAccount, AUTH_TOKEN_TYPE, oAuthResponse.accessToken)
}
}

// Save credentials
CoroutineScope(dispatcherProvider.io()).launch {
secureSharedPreference.saveCredentials(username, password)
}
secureSharedPreference.saveCredentials(username, password)
}
}

Expand Down

0 comments on commit 61412d8

Please sign in to comment.