From 3f39c709bb175848d41d1aac65ac20fb242ca0da Mon Sep 17 00:00:00 2001 From: JuliaJakubcova Date: Mon, 16 Feb 2026 12:36:37 +0100 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20[KMP]=20Refactor=20and=20s?= =?UTF-8?q?implify=20refresh=20token=20login=20in=20AuthProviderImpl?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../auth/data/provider/AuthProviderImpl.kt | 31 ++++++------------- 1 file changed, 9 insertions(+), 22 deletions(-) diff --git a/shared/auth/src/commonMain/kotlin/kmp/shared/auth/data/provider/AuthProviderImpl.kt b/shared/auth/src/commonMain/kotlin/kmp/shared/auth/data/provider/AuthProviderImpl.kt index e121ae99..415cf068 100644 --- a/shared/auth/src/commonMain/kotlin/kmp/shared/auth/data/provider/AuthProviderImpl.kt +++ b/shared/auth/src/commonMain/kotlin/kmp/shared/auth/data/provider/AuthProviderImpl.kt @@ -4,11 +4,10 @@ import com.russhwolf.settings.Settings import com.russhwolf.settings.set import kmp.shared.auth.data.remote.TokenRefresher import kmp.shared.base.data.provider.AuthProvider -import kotlinx.coroutines.Deferred -import kotlinx.coroutines.async -import kotlinx.coroutines.coroutineScope +import kotlinx.coroutines.NonCancellable import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.withLock +import kotlinx.coroutines.withContext internal class AuthProviderImpl( private val settings: Settings, @@ -20,29 +19,17 @@ internal class AuthProviderImpl( set(value) = settings.set(TOKEN_KEY, value) private val mutex = Mutex() - private var inFlight: Deferred? = null - override suspend fun refreshToken(): String? = coroutineScope { - mutex.withLock { - inFlight?.let { return@coroutineScope it.await() } + override suspend fun refreshToken(): String? { + val oldToken = token - val task = async { - val fresh = tokenRefresher.refresh() - token = fresh - fresh - } - inFlight = task - task - }.let { task -> - try { - task.await() - } finally { - mutex.withLock { - if (inFlight === task) { - inFlight = null - } + mutex.withLock { + if (oldToken == token) { + withContext(NonCancellable) { + token = tokenRefresher.refresh() } } + return token } }