Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] 토큰 재발급 시 앱 크래시 현상 해결 #353

Merged
merged 2 commits into from
Jun 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ package com.runnect.runnect.data.service

import android.content.Context
import com.runnect.runnect.application.ApplicationClass
import com.runnect.runnect.data.dto.response.ResponseGetRefreshToken
import com.runnect.runnect.data.dto.response.base.BaseResponse
import com.runnect.runnect.data.dto.response.Token
import com.runnect.runnect.util.preference.AuthUtil.getAccessToken
import com.runnect.runnect.util.preference.AuthUtil.getNewToken
import com.runnect.runnect.util.preference.AuthUtil.saveToken
Expand All @@ -13,7 +12,6 @@ import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json
import okhttp3.Interceptor
import okhttp3.Request
import okhttp3.RequestBody.Companion.toRequestBody
import okhttp3.Response
import timber.log.Timber
import javax.inject.Inject
Expand Down Expand Up @@ -76,12 +74,13 @@ class AuthInterceptor @Inject constructor(

private fun getNewToken(originalRequest: Request, chain: Interceptor.Chain): Response {
val baseUrl = ApplicationClass.getBaseUrl()
val accessToken = context.getAccessToken()
val refreshToken = context.getNewToken()
val refreshTokenRequest = originalRequest.newBuilder().post("".toRequestBody())
val refreshTokenRequest = originalRequest.newBuilder().get()
.url("$baseUrl/api/auth/getNewToken")
.addHeader(ACCESS_TOKEN, accessToken)
.addHeader(REFRESH_TOKEN, refreshToken)
.build()

return chain.proceed(refreshTokenRequest)
}

Expand All @@ -91,10 +90,10 @@ class AuthInterceptor @Inject constructor(
chain: Interceptor.Chain
): Response {
refreshTokenResponse.use { response ->
val responseToken = json.decodeFromString<BaseResponse<ResponseGetRefreshToken>>(
val responseToken = json.decodeFromString<Token>(
response.body?.string().orEmpty()
)
responseToken.data?.data?.let {
responseToken.let {
Timber.e("New Refresh Token Success: ${it.refreshToken}")
context.saveToken(it.accessToken, it.refreshToken)
}
Expand Down
Loading