Skip to content

Commit

Permalink
[FIX/#68] AuthInterceptor 수정사항 대응
Browse files Browse the repository at this point in the history
  • Loading branch information
Marchbreeze committed Aug 13, 2024
1 parent 84d64c8 commit 21c2c91
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import android.content.Intent
import android.os.Handler
import android.os.Looper
import co.orange.core.extension.toast
import co.orange.data.local.UserSharedPref
import co.orange.domain.entity.request.AuthTokenRequestModel
import co.orange.domain.entity.request.ReissueRequestModel
import co.orange.domain.repository.AuthRepository
import co.orange.domain.repository.UserRepository
import co.orange.presentation.auth.login.LoginActivity
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.runBlocking
Expand All @@ -21,64 +21,62 @@ class AuthInterceptor
@Inject
constructor(
private val authRepository: AuthRepository,
private val sharedPref: UserSharedPref,
private val userRepository: UserRepository,
@ApplicationContext private val context: Context,
) : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val originalRequest = chain.request()

Timber.d("GET ACCESS TOKEN : ${sharedPref.accessToken}")
Timber.tag("okhttp").d("ACCESS TOKEN : ${userRepository.getAccessToken()}")

val authRequest =
if (sharedPref.accessToken.isNotBlank()) {
if (userRepository.getAccessToken().isNotBlank()) {
originalRequest.newBuilder().newAuthBuilder().build()
} else {
originalRequest
}

val response = chain.proceed(authRequest)

when (response.code) {
CODE_TOKEN_EXPIRED -> {
try {
runBlocking {
authRepository.postReissueTokens(
sharedPref.refreshToken,
AuthTokenRequestModel(0),
)
}.onSuccess { data ->
sharedPref.apply {
accessToken = data.accessToken
refreshToken = data.refreshToken
}
if (response.code == CODE_TOKEN_EXPIRED) {
try {
runBlocking {
authRepository.postReissueTokens(
ReissueRequestModel(
userRepository.getAccessToken(),
userRepository.getRefreshToken(),
),
)
}.onSuccess { data ->
userRepository.setTokens(
data.accessToken,
data.refreshToken,
)
response.close()

response.close()

val newRequest =
authRequest.newBuilder().removeHeader(AUTHORIZATION).newAuthBuilder()
.build()

return chain.proceed(newRequest)
}
} catch (t: Throwable) {
Timber.d(t.message)
val newRequest =
authRequest.newBuilder().removeHeader(AUTHORIZATION).newAuthBuilder()
.build()
return chain.proceed(newRequest)
}
} catch (t: Throwable) {
Timber.tag("okhttp").d(t)
}

sharedPref.clearInfo()
userRepository.clearInfo()

Handler(Looper.getMainLooper()).post {
context.toast(TOKEN_EXPIRED_ERROR)
Intent(context, LoginActivity::class.java).apply {
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK)
context.startActivity(this)
}
Handler(Looper.getMainLooper()).post {
context.toast(TOKEN_EXPIRED_ERROR)
Intent(context, LoginActivity::class.java).apply {
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK)
context.startActivity(this)
}
}
}
return response
}

private fun Request.Builder.newAuthBuilder() = this.addHeader(AUTHORIZATION, "$BEARER ${sharedPref.accessToken}")
private fun Request.Builder.newAuthBuilder() = this.addHeader(AUTHORIZATION, "$BEARER ${userRepository.getAccessToken()}")

companion object {
private const val CODE_TOKEN_EXPIRED = 401
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import kotlinx.serialization.Serializable

@Serializable
data class ReissueRequestDto(
@SerialName("userId")
val userId: Long,
@SerialName("accessToken")
val accessToken: String,
@SerialName("refreshToken")
val refreshToken: String,
) {
companion object {
fun ReissueRequestModel.toDto() = ReissueRequestDto(userId)
fun ReissueRequestModel.toDto() = ReissueRequestDto(accessToken, refreshToken)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package co.orange.domain.entity.request

data class ReissueRequestModel(
val userId: Long,
val accessToken: String,
val refreshToken: String,
)

0 comments on commit 21c2c91

Please sign in to comment.