-
Notifications
You must be signed in to change notification settings - Fork 3
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
feat: network module 을 사용한 401 refresh 방식 수정 #697
base: chongdae
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.zzang.chongdae.data.remote.interceptor | ||
|
||
import com.zzang.chongdae.common.handler.Result | ||
import com.zzang.chongdae.domain.usecase.auth.RefreshTokenUseCase | ||
import dagger.Lazy | ||
import kotlinx.coroutines.runBlocking | ||
import okhttp3.Authenticator | ||
import okhttp3.Request | ||
import okhttp3.Response | ||
import okhttp3.Route | ||
import javax.inject.Inject | ||
|
||
class TokenAuthenticator | ||
@Inject | ||
constructor( | ||
private val refreshTokenUseCase: Lazy<RefreshTokenUseCase>, | ||
) : Authenticator { | ||
override fun authenticate( | ||
route: Route?, | ||
response: Response, | ||
): Request? { | ||
if (response.request.header(AUTHORIZATION_HEADER) != null) { | ||
return null | ||
} | ||
|
||
val newToken = | ||
runBlocking { | ||
when (val result = refreshTokenUseCase.get()()) { | ||
is Result.Success -> result.data | ||
else -> null | ||
} | ||
} ?: return null | ||
|
||
return response.request.newBuilder() | ||
.header(AUTHORIZATION_HEADER, "$TOKEN_PREFIX$newToken") | ||
.build() | ||
} | ||
Comment on lines
+26
to
+37
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 부분이 무한 refresh api에 문제가 있어서 무한 refresh에 빠지는 것을 막는 부분인가여?? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 맞습니당 AUTHORIZATION_HEADER을 생성해주어서 refresh 를 진행한 애들에게는 AUTHORIZATION_HEADER 값이 null 이 아닌 것으로 감지하여 무한 루프에 빠지는 것을 방지했습니다! |
||
|
||
companion object { | ||
private const val AUTHORIZATION_HEADER = "Authorization" | ||
private const val TOKEN_PREFIX = "Bearer " | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이미 Authorization 헤더가 있는 경우에는 refreshTokenUseCase가 필요없으니 Lazy를 사용하신건가용
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
아 Lazy 에 대한 코드 내용 설명이 빠졌네요. 지금 NetworkModule은 refreshTokenUseCase가 필요한데,
NetworkModule에서 auth 관련 api service를 provide해주고 있습니다. 이 때문에
순환 참조 문제
가 일어나기 때문에 사용할때 refreshTokenUseCase를 불러오도록 Lazy를 사용하였습니다.