-
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?
Conversation
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.
작업하시느라 고생하셨습니다.
로직들이 캡슐화되면서, 불필요한 코드들이 없어지고 가독성이 올라가서 훨씬 사용하기 편해졌네요!👍
고칠만한 부분은 없었구여, 코드 보면서 궁금한 것들 질문 몇개만 남겼습니당
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() | ||
} |
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.
이 부분이 무한 refresh api에 문제가 있어서 무한 refresh에 빠지는 것을 막는 부분인가여??
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_HEADER을 생성해주어서 refresh 를 진행한 애들에게는 AUTHORIZATION_HEADER 값이 null 이 아닌 것으로 감지하여 무한 루프에 빠지는 것을 방지했습니다!
@Module | ||
@InstallIn(SingletonComponent::class) | ||
object NetworkModule { |
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.
이거 만든거 너무 좋네요
덕분에 불필요한 DI모듈 코드가 없어지고 훨씬 가독성이 좋아졌네요
👍👍
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.
이제 refresh 지옥 해방!
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 | ||
} | ||
|
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를 사용하였습니다.
📌 관련 이슈
close #696
✨ 작업 내용
인증 토큰 갱신 로직을 RefreshTokenUseCase에 캡슐화하여 토큰 갱신 관련 로직이 한 곳에 모이고 재사용 쉽도록 코드를 수정하였습니다.
hilt를 사용하고 있는 만큼 NetworkManager -> NetworkModule을 만들어 hilt가 API service를 주입하도록 변경하였습니다.
TokenAuthenticator
이미 Authorization 헤더가 있는 경우 중단 (재시도 방지)
AUTHORIZATION_HEADER을 추가하여 401이 안될 경우 계속해서 무한 루프에 빠지는 것을 막았습니다.
📚 기타
패키지 구조를 고민하다가 인증과 관련된 로직이 api와는 살짝 성격이 다른 것 같아서 패키지를 새로 만들었습니다.
코드 이상한 부분을 못찾겠는데 자꾸 의존성 주입이 안되어서 하루간 뻘짓하다가 캐시 삭제하니 바로 되네여…..ㅎ 유의하세요
https://jwt.io/ 에서 만료된 AccessToken을 생성해서 디버그 상황에서는 제대로 refresh가 되는 것을 확인했으나 추후에 test 추가한다면 더 좋을 듯함다