From 845770efe271c6620331a26d21073e421f9b981a Mon Sep 17 00:00:00 2001 From: buna Date: Wed, 8 Nov 2023 12:59:31 +0900 Subject: [PATCH 1/3] =?UTF-8?q?feat:=20=EC=9D=91=EB=8B=B5=20=EC=BD=94?= =?UTF-8?q?=EB=93=9C=EA=B0=80=20401=EC=9D=B4=EB=A9=B4=20=EB=A1=9C=EA=B7=B8?= =?UTF-8?q?=EC=9D=B8=20=ED=99=94=EB=A9=B4=EC=9C=BC=EB=A1=9C=20=EC=9D=B4?= =?UTF-8?q?=EB=8F=99=ED=95=98=EB=8A=94=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../data/common/retrofit/AuthInterceptor.kt | 33 ++++++++++++++++--- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/android/2023-emmsale/app/src/main/java/com/emmsale/data/common/retrofit/AuthInterceptor.kt b/android/2023-emmsale/app/src/main/java/com/emmsale/data/common/retrofit/AuthInterceptor.kt index 9508e40ed..f543129be 100644 --- a/android/2023-emmsale/app/src/main/java/com/emmsale/data/common/retrofit/AuthInterceptor.kt +++ b/android/2023-emmsale/app/src/main/java/com/emmsale/data/common/retrofit/AuthInterceptor.kt @@ -1,25 +1,48 @@ package com.emmsale.data.common.retrofit import android.content.Context +import android.content.Intent import com.emmsale.data.repository.interfaces.TokenRepository +import com.emmsale.presentation.ui.login.LoginActivity import dagger.hilt.EntryPoint import dagger.hilt.InstallIn import dagger.hilt.android.EntryPointAccessors import dagger.hilt.components.SingletonComponent import okhttp3.Interceptor +import okhttp3.Request import okhttp3.Response -class AuthInterceptor(context: Context) : Interceptor { +class AuthInterceptor(private val context: Context) : Interceptor { private val tokenRepository = EntryPointAccessors .fromApplication(context) .getTokenRepository() override fun intercept(chain: Interceptor.Chain): Response { val token = tokenRepository.getToken() - val newRequest = chain.request().newBuilder() - .addHeader(ACCESS_TOKEN_HEADER, ACCESS_TOKEN_FORMAT.format(token?.accessToken)) - .build() - return chain.proceed(newRequest) + val tokenAddedRequest = chain.request().putAccessToken(token?.accessToken) + + val response = chain.proceed(tokenAddedRequest) + if (response.isAccessTokenExpired()) { + tokenRepository.deleteToken() + navigateToLogin() + } + return response + } + + private fun Response.isAccessTokenExpired(): Boolean = (code == 401) + + private fun Request.putAccessToken(token: String?): Request = + putHeader(ACCESS_TOKEN_HEADER, ACCESS_TOKEN_FORMAT.format(token)) + + private fun Request.putHeader( + key: String, + value: String, + ): Request = newBuilder().addHeader(key, value).build() + + private fun navigateToLogin() { + val loginStartIntent = Intent(context, LoginActivity::class.java) + .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK) + context.startActivity(loginStartIntent) } @EntryPoint From d17af7309e6ad8bbdfee91b5123f6cf280be2ea4 Mon Sep 17 00:00:00 2001 From: buna Date: Wed, 8 Nov 2023 13:46:31 +0900 Subject: [PATCH 2/3] =?UTF-8?q?feat:=20=ED=86=A0=ED=81=B0=EC=9D=B4=20?= =?UTF-8?q?=EB=A7=8C=EB=A3=8C=EB=90=98=EC=97=88=EC=9D=84=20=EB=95=8C=20?= =?UTF-8?q?=ED=86=A0=ED=81=B0=EC=9D=84=20=EC=A0=9C=EA=B1=B0=ED=95=98?= =?UTF-8?q?=EB=8A=94=20=EB=A1=9C=EC=A7=81=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/emmsale/data/common/retrofit/AuthInterceptor.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/android/2023-emmsale/app/src/main/java/com/emmsale/data/common/retrofit/AuthInterceptor.kt b/android/2023-emmsale/app/src/main/java/com/emmsale/data/common/retrofit/AuthInterceptor.kt index f543129be..a9694b332 100644 --- a/android/2023-emmsale/app/src/main/java/com/emmsale/data/common/retrofit/AuthInterceptor.kt +++ b/android/2023-emmsale/app/src/main/java/com/emmsale/data/common/retrofit/AuthInterceptor.kt @@ -23,7 +23,6 @@ class AuthInterceptor(private val context: Context) : Interceptor { val response = chain.proceed(tokenAddedRequest) if (response.isAccessTokenExpired()) { - tokenRepository.deleteToken() navigateToLogin() } return response From fe4b8628b3fe147b5d0b45a529b97709e387f84a Mon Sep 17 00:00:00 2001 From: buna Date: Wed, 8 Nov 2023 15:31:00 +0900 Subject: [PATCH 3/3] =?UTF-8?q?refactor:=20isAccessTokenInvalid=EB=A1=9C?= =?UTF-8?q?=20=EB=A9=94=EC=84=9C=EB=93=9C=EB=AA=85=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/emmsale/data/common/retrofit/AuthInterceptor.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/android/2023-emmsale/app/src/main/java/com/emmsale/data/common/retrofit/AuthInterceptor.kt b/android/2023-emmsale/app/src/main/java/com/emmsale/data/common/retrofit/AuthInterceptor.kt index a9694b332..326c77553 100644 --- a/android/2023-emmsale/app/src/main/java/com/emmsale/data/common/retrofit/AuthInterceptor.kt +++ b/android/2023-emmsale/app/src/main/java/com/emmsale/data/common/retrofit/AuthInterceptor.kt @@ -22,13 +22,13 @@ class AuthInterceptor(private val context: Context) : Interceptor { val tokenAddedRequest = chain.request().putAccessToken(token?.accessToken) val response = chain.proceed(tokenAddedRequest) - if (response.isAccessTokenExpired()) { + if (response.isAccessTokenInvalid()) { navigateToLogin() } return response } - private fun Response.isAccessTokenExpired(): Boolean = (code == 401) + private fun Response.isAccessTokenInvalid(): Boolean = (code == 401) private fun Request.putAccessToken(token: String?): Request = putHeader(ACCESS_TOKEN_HEADER, ACCESS_TOKEN_FORMAT.format(token))