Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.moive.app.core.network

import com.moive.app.core.network.token.AuthManager
import com.moive.app.core.network.token.TokenReissueLock
import com.moive.app.data.auth.repository.AuthRepository
import com.moive.app.data.local.token.LocalTokenDataSource
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import okhttp3.Authenticator
import okhttp3.Request
Expand All @@ -18,10 +18,9 @@ class TokenAuthenticator @Inject constructor(
private val authRepository: AuthRepository,
private val tokenDataStore: LocalTokenDataSource,
private val authManager: AuthManager,
private val tokenReissueLock: TokenReissueLock,
) : Authenticator {

private val mutex = Mutex()

override fun authenticate(route: Route?, response: Response): Request? {
if (responseCount(response) >= MAX_RESPONSE_COUNT) return null

Expand All @@ -30,7 +29,7 @@ class TokenAuthenticator @Inject constructor(
}
}

private suspend fun updateToken(response: Response): Request? = mutex.withLock {
private suspend fun updateToken(response: Response): Request? = tokenReissueLock.mutex.withLock {
val accessToken = tokenDataStore.getAccessToken()
val oldAccessToken =
response.request.header(AUTHORIZATION)?.replace("$BEARER_SUFFIX ", "")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.moive.app.core.network.token

import kotlinx.coroutines.sync.Mutex
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class TokenReissueLock @Inject constructor() {
val mutex = Mutex()
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.moive.app.presentation.splash
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.moive.app.core.designsystem.component.toast.ToastType
import com.moive.app.core.network.token.TokenReissueLock
import com.moive.app.core.utils.suspendRunCatching
import com.moive.app.data.auth.repository.AuthRepository
import com.moive.app.presentation.splash.SplashContract.SideEffect.NavigateToHome
Expand All @@ -16,6 +17,7 @@ import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.receiveAsFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import kotlinx.coroutines.sync.withLock
import timber.log.Timber
import java.io.IOException
import javax.inject.Inject
Expand All @@ -24,6 +26,7 @@ import kotlin.time.Duration.Companion.milliseconds
@HiltViewModel
class SplashViewModel @Inject constructor(
private val authRepository: AuthRepository,
private val tokenReissueLock: TokenReissueLock,
) : ViewModel() {

private val _uiState = MutableStateFlow(SplashContract.State())
Expand Down Expand Up @@ -64,7 +67,7 @@ class SplashViewModel @Inject constructor(


private suspend fun postReissue() {
authRepository.postReissue()
tokenReissueLock.mutex.withLock { authRepository.postReissue() }
.onSuccess {
Timber.tag(AUTHORIZATION).d(REISSUE_SUCCESS_MESSAGE)
}
Expand Down
Loading