From 8ce6e60441658b07dc239e2a48c696a8549529b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EB=B0=95=EC=84=B1=ED=98=84?= <84944098+Cjsghkd@users.noreply.github.com> Date: Mon, 1 May 2023 14:45:13 +0900 Subject: [PATCH 1/4] :recycle: refactoring Auto login logic --- .../remote/dto/auth/request/RefreshRequest.kt | 5 ++++- .../gcms/data/remote/network/LoginInterceptor.kt | 15 +++++++++------ .../java/com/msg/gcms/di/module/NetworkModule.kt | 9 +++++++++ 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/com/msg/gcms/data/remote/dto/auth/request/RefreshRequest.kt b/app/src/main/java/com/msg/gcms/data/remote/dto/auth/request/RefreshRequest.kt index 66793076..40a24a00 100644 --- a/app/src/main/java/com/msg/gcms/data/remote/dto/auth/request/RefreshRequest.kt +++ b/app/src/main/java/com/msg/gcms/data/remote/dto/auth/request/RefreshRequest.kt @@ -3,6 +3,7 @@ package com.msg.gcms.data.remote.dto.auth.request import com.google.gson.annotations.SerializedName import okhttp3.MediaType.Companion.toMediaTypeOrNull import okhttp3.RequestBody.Companion.toRequestBody +import org.json.JSONObject data class RefreshRequest( @SerializedName("token") @@ -10,4 +11,6 @@ data class RefreshRequest( ) fun RefreshRequest.toRequestBody() = - this.toString().toRequestBody("application/json".toMediaTypeOrNull()) \ No newline at end of file + JSONObject().apply { + put("token", this@toRequestBody.token) + }.toString().toRequestBody("application/json".toMediaTypeOrNull()) \ No newline at end of file diff --git a/app/src/main/java/com/msg/gcms/data/remote/network/LoginInterceptor.kt b/app/src/main/java/com/msg/gcms/data/remote/network/LoginInterceptor.kt index 85f89852..650e96f3 100644 --- a/app/src/main/java/com/msg/gcms/data/remote/network/LoginInterceptor.kt +++ b/app/src/main/java/com/msg/gcms/data/remote/network/LoginInterceptor.kt @@ -24,12 +24,8 @@ class LoginInterceptor @Inject constructor( val request = chain.request() val path = request.url.encodedPath val method = request.method - val ignorePath = listOf( - "/auth" - ) - val ignoreMethod = listOf( - "POST" - ) + val ignorePath = listOf("/auth") + val ignoreMethod = listOf("POST") ignorePath.forEachIndexed { index, s -> if (s == path && ignoreMethod[index] == method) { @@ -66,6 +62,13 @@ class LoginInterceptor @Inject constructor( authDataStorage.setRefreshToken(token["refreshToken"].toString().removeDot()) authDataStorage.setAccessExpiredAt(token["accessExp"].toString().removeDot()) authDataStorage.setRefreshExpiredAt(token["refreshExp"].toString().removeDot()) + + val reAccessToken = authDataStorage.getAccessToken() + val reAccessRequest = chain.request() + .newBuilder() + .addHeader("Authorization", "Bearer $reAccessToken") + .build() + return chain.proceed(reAccessRequest) } else throw NeedLoginException() } diff --git a/app/src/main/java/com/msg/gcms/di/module/NetworkModule.kt b/app/src/main/java/com/msg/gcms/di/module/NetworkModule.kt index a6df4f37..9ed40f8b 100644 --- a/app/src/main/java/com/msg/gcms/di/module/NetworkModule.kt +++ b/app/src/main/java/com/msg/gcms/di/module/NetworkModule.kt @@ -1,5 +1,6 @@ package com.msg.gcms.di.module +import android.util.Log import com.msg.gcms.BuildConfig import com.msg.gcms.data.remote.network.api.ClubAPI import com.msg.gcms.data.remote.network.api.AuthAPI @@ -13,6 +14,7 @@ import dagger.Provides import dagger.hilt.InstallIn import dagger.hilt.components.SingletonComponent import okhttp3.OkHttpClient +import okhttp3.logging.HttpLoggingInterceptor import retrofit2.Retrofit import retrofit2.converter.gson.GsonConverterFactory import java.util.concurrent.TimeUnit @@ -22,9 +24,15 @@ import javax.inject.Singleton @InstallIn(SingletonComponent::class) object NetworkModule { + @Provides + fun provideHttpLoggingInterceptor(): HttpLoggingInterceptor = + HttpLoggingInterceptor { message -> Log.v("HTTP", message) } + .setLevel(HttpLoggingInterceptor.Level.BODY) + @Provides @Singleton fun provideOkhttpClient( + httpLoggingInterceptor: HttpLoggingInterceptor, loginInterceptor: LoginInterceptor ): OkHttpClient { return OkHttpClient.Builder() @@ -35,6 +43,7 @@ object NetworkModule { // 읽기 타임 아웃의 반대 방향. 얼마나 빨리 서버에 바이트를 보낼 수 있는지 확인 .writeTimeout(30, TimeUnit.SECONDS) .addInterceptor(loginInterceptor) + .addInterceptor(httpLoggingInterceptor) .build() } From 2960b7283b668b4a9923716c1e505c3bb5c9e63c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EB=B0=95=EC=84=B1=ED=98=84?= <84944098+Cjsghkd@users.noreply.github.com> Date: Tue, 2 May 2023 10:41:14 +0900 Subject: [PATCH 2/4] =?UTF-8?q?:recycle:=20=EC=BD=94=EB=93=9C=20=EC=BB=A8?= =?UTF-8?q?=EB=B2=A4=EC=85=98=20=EB=A7=9E=EC=B6=94=EA=B8=B0=20(=20=3D=20->?= =?UTF-8?q?=20{=20return=20}=20)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../gcms/data/remote/dto/auth/request/RefreshRequest.kt | 8 +++++--- app/src/main/java/com/msg/gcms/di/module/NetworkModule.kt | 5 +++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/com/msg/gcms/data/remote/dto/auth/request/RefreshRequest.kt b/app/src/main/java/com/msg/gcms/data/remote/dto/auth/request/RefreshRequest.kt index 40a24a00..45a799c8 100644 --- a/app/src/main/java/com/msg/gcms/data/remote/dto/auth/request/RefreshRequest.kt +++ b/app/src/main/java/com/msg/gcms/data/remote/dto/auth/request/RefreshRequest.kt @@ -2,6 +2,7 @@ package com.msg.gcms.data.remote.dto.auth.request import com.google.gson.annotations.SerializedName import okhttp3.MediaType.Companion.toMediaTypeOrNull +import okhttp3.RequestBody import okhttp3.RequestBody.Companion.toRequestBody import org.json.JSONObject @@ -10,7 +11,8 @@ data class RefreshRequest( val token: String ) -fun RefreshRequest.toRequestBody() = - JSONObject().apply { +fun RefreshRequest.toRequestBody(): RequestBody { + return JSONObject().apply { put("token", this@toRequestBody.token) - }.toString().toRequestBody("application/json".toMediaTypeOrNull()) \ No newline at end of file + }.toString().toRequestBody("application/json".toMediaTypeOrNull()) +} \ No newline at end of file diff --git a/app/src/main/java/com/msg/gcms/di/module/NetworkModule.kt b/app/src/main/java/com/msg/gcms/di/module/NetworkModule.kt index 9ed40f8b..9eb4e105 100644 --- a/app/src/main/java/com/msg/gcms/di/module/NetworkModule.kt +++ b/app/src/main/java/com/msg/gcms/di/module/NetworkModule.kt @@ -25,9 +25,10 @@ import javax.inject.Singleton object NetworkModule { @Provides - fun provideHttpLoggingInterceptor(): HttpLoggingInterceptor = - HttpLoggingInterceptor { message -> Log.v("HTTP", message) } + fun provideHttpLoggingInterceptor(): HttpLoggingInterceptor { + return HttpLoggingInterceptor { message -> Log.v("HTTP", message) } .setLevel(HttpLoggingInterceptor.Level.BODY) + } @Provides @Singleton From 352721d79bb1598802279143d371c1a4524e6238 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EB=B0=95=EC=84=B1=ED=98=84?= <84944098+Cjsghkd@users.noreply.github.com> Date: Tue, 2 May 2023 10:56:29 +0900 Subject: [PATCH 3/4] =?UTF-8?q?:fire:=20=EB=8F=99=EA=B8=B0=20=EC=B2=98?= =?UTF-8?q?=EB=A6=AC=20=EC=95=88=EC=97=90=20=EC=9E=88=EB=8D=98=20return=20?= =?UTF-8?q?=EC=A0=9C=EA=B1=B0=20=ED=95=98=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/msg/gcms/data/remote/network/LoginInterceptor.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/com/msg/gcms/data/remote/network/LoginInterceptor.kt b/app/src/main/java/com/msg/gcms/data/remote/network/LoginInterceptor.kt index 650e96f3..aed04506 100644 --- a/app/src/main/java/com/msg/gcms/data/remote/network/LoginInterceptor.kt +++ b/app/src/main/java/com/msg/gcms/data/remote/network/LoginInterceptor.kt @@ -68,7 +68,7 @@ class LoginInterceptor @Inject constructor( .newBuilder() .addHeader("Authorization", "Bearer $reAccessToken") .build() - return chain.proceed(reAccessRequest) + chain.proceed(reAccessRequest) } else throw NeedLoginException() } From c1e6cad7a0d58fc9246a93b39ed7910ec2928d7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EB=B0=95=EC=84=B1=ED=98=84?= <84944098+Cjsghkd@users.noreply.github.com> Date: Tue, 2 May 2023 11:07:42 +0900 Subject: [PATCH 4/4] =?UTF-8?q?:fire:=20reAccessToken=20=EB=A1=9C=EC=A7=81?= =?UTF-8?q?=20=EC=82=AD=EC=A0=9C=20=ED=95=98=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/msg/gcms/data/remote/network/LoginInterceptor.kt | 7 ------- 1 file changed, 7 deletions(-) diff --git a/app/src/main/java/com/msg/gcms/data/remote/network/LoginInterceptor.kt b/app/src/main/java/com/msg/gcms/data/remote/network/LoginInterceptor.kt index aed04506..811fc889 100644 --- a/app/src/main/java/com/msg/gcms/data/remote/network/LoginInterceptor.kt +++ b/app/src/main/java/com/msg/gcms/data/remote/network/LoginInterceptor.kt @@ -62,13 +62,6 @@ class LoginInterceptor @Inject constructor( authDataStorage.setRefreshToken(token["refreshToken"].toString().removeDot()) authDataStorage.setAccessExpiredAt(token["accessExp"].toString().removeDot()) authDataStorage.setRefreshExpiredAt(token["refreshExp"].toString().removeDot()) - - val reAccessToken = authDataStorage.getAccessToken() - val reAccessRequest = chain.request() - .newBuilder() - .addHeader("Authorization", "Bearer $reAccessToken") - .build() - chain.proceed(reAccessRequest) } else throw NeedLoginException() }