Skip to content

Commit

Permalink
Merge remote-tracking branch 'refs/remotes/origin/feat-token-reissue'…
Browse files Browse the repository at this point in the history
… into feat-edit-profile-api
  • Loading branch information
t1nm1ksun committed Aug 17, 2024
2 parents 63bf451 + 83ff619 commit ac8e6e5
Showing 1 changed file with 40 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.sopt.dateroad.data.dataremote.interceptor
import android.app.Application
import android.content.Intent
import java.lang.IllegalStateException
import java.util.concurrent.atomic.AtomicBoolean
import javax.inject.Inject
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
Expand All @@ -26,52 +27,60 @@ class AuthInterceptor @Inject constructor(
private val context: Application
) : Interceptor {

private val isRefreshing = AtomicBoolean(false)

override fun intercept(chain: Interceptor.Chain): Response {
val originalRequest = chain.request()
val authRequest =
if (localStorage.accessToken.isNotBlank()) originalRequest.newAuthBuilder() else originalRequest
val response = chain.proceed(authRequest)
var response = chain.proceed(authRequest)

when (response.code) {
CODE_TOKEN_EXPIRE -> {
response.close()
val refreshTokenRequest = originalRequest.newBuilder().get()
.url("${BuildConfig.BASE_URL}$API/$VERSION/$USERS/$REISSUE")
.patch("".toRequestBody(null))
.addHeader(AUTHORIZATION, localStorage.refreshToken)
.build()
val refreshTokenResponse = chain.proceed(refreshTokenRequest)
if (response.code == CODE_TOKEN_EXPIRE) {
response.close()

if (refreshTokenResponse.isSuccessful) {
val responseRefresh =
json.decodeFromString<ResponseRefreshTokenDto>(
refreshTokenResponse.body?.string()
?: throw IllegalStateException("\"refreshTokenResponse is null $refreshTokenResponse\"")
)
if (isRefreshing.compareAndSet(false, true)) {
try {
val refreshTokenRequest = originalRequest.newBuilder().get()
.url("${BuildConfig.BASE_URL}$API/$VERSION/$USERS/$REISSUE")
.patch("".toRequestBody(null))
.addHeader(AUTHORIZATION, localStorage.refreshToken)
.build()
val refreshTokenResponse = chain.proceed(refreshTokenRequest)

with(localStorage) {
accessToken = "$BEARER${responseRefresh.accessToken}"
refreshToken = "$BEARER${responseRefresh.refreshToken}"
}
if (refreshTokenResponse.isSuccessful) {
val responseRefresh =
json.decodeFromString<ResponseRefreshTokenDto>(
refreshTokenResponse.body?.string()
?: throw IllegalStateException("\"refreshTokenResponse is null $refreshTokenResponse\"")
)

refreshTokenResponse.close()
with(localStorage) {
accessToken = BEARER + responseRefresh.accessToken
refreshToken = BEARER + responseRefresh.refreshToken
}

val newRequest = originalRequest.newAuthBuilder()
return chain.proceed(newRequest)
} else {
with(context) {
CoroutineScope(Dispatchers.Main).launch {
startActivity(
Intent.makeRestartActivityTask(
packageManager.getLaunchIntentForPackage(packageName)?.component
refreshTokenResponse.close()
} else {
with(context) {
CoroutineScope(Dispatchers.Main).launch {
startActivity(
Intent.makeRestartActivityTask(
packageManager.getLaunchIntentForPackage(packageName)?.component
)
)
)
localStorage.clear()
localStorage.clear()
}
}
}
} finally {
isRefreshing.set(false)
}
}

val newRequest = originalRequest.newAuthBuilder()
return chain.proceed(newRequest)
}

return response
}

Expand All @@ -80,8 +89,6 @@ class AuthInterceptor @Inject constructor(

companion object {
const val CODE_TOKEN_EXPIRE = 401
const val CONTENT_TYPE = "Content-Type"
const val APPLICATION_JSON = "application/json"
const val AUTHORIZATION = "Authorization"
const val BEARER = "Bearer "
}
Expand Down

0 comments on commit ac8e6e5

Please sign in to comment.