From 21c2c9108e5b627798884373fb52d0b9f095eb3d Mon Sep 17 00:00:00 2001 From: Sangho Kim Date: Tue, 13 Aug 2024 21:57:04 +0900 Subject: [PATCH] =?UTF-8?q?[FIX/#68]=20AuthInterceptor=20=EC=88=98?= =?UTF-8?q?=EC=A0=95=EC=82=AC=ED=95=AD=20=EB=8C=80=EC=9D=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ddanzi/di/interceptor/AuthInterceptor.kt | 70 +++++++++---------- .../data/dto/request/ReissueRequestDto.kt | 8 ++- .../entity/request/ReissueRequestModel.kt | 3 +- 3 files changed, 41 insertions(+), 40 deletions(-) diff --git a/app/src/main/java/co/orange/ddanzi/di/interceptor/AuthInterceptor.kt b/app/src/main/java/co/orange/ddanzi/di/interceptor/AuthInterceptor.kt index 2dc08e66..06fbe6d1 100644 --- a/app/src/main/java/co/orange/ddanzi/di/interceptor/AuthInterceptor.kt +++ b/app/src/main/java/co/orange/ddanzi/di/interceptor/AuthInterceptor.kt @@ -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 @@ -21,16 +21,16 @@ 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 @@ -38,47 +38,45 @@ class AuthInterceptor 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 diff --git a/data/src/main/java/co/orange/data/dto/request/ReissueRequestDto.kt b/data/src/main/java/co/orange/data/dto/request/ReissueRequestDto.kt index b232cd2f..71e1472d 100644 --- a/data/src/main/java/co/orange/data/dto/request/ReissueRequestDto.kt +++ b/data/src/main/java/co/orange/data/dto/request/ReissueRequestDto.kt @@ -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) } } diff --git a/domain/src/main/kotlin/co/orange/domain/entity/request/ReissueRequestModel.kt b/domain/src/main/kotlin/co/orange/domain/entity/request/ReissueRequestModel.kt index 3508aa21..fb0e1ad6 100644 --- a/domain/src/main/kotlin/co/orange/domain/entity/request/ReissueRequestModel.kt +++ b/domain/src/main/kotlin/co/orange/domain/entity/request/ReissueRequestModel.kt @@ -1,5 +1,6 @@ package co.orange.domain.entity.request data class ReissueRequestModel( - val userId: Long, + val accessToken: String, + val refreshToken: String, )