Skip to content

Commit

Permalink
[AN/USER] fix: refresh 토큰 인증 오류 수정(#941) (#942)
Browse files Browse the repository at this point in the history
* fix : refresh 요청 전에 토큰을 파기한다.

* feat : app version up 11 (2.0.2)

* feat : 401 오류 발생 시 토큰을 지운다

* refactor: ktlint check
  • Loading branch information
SeongHoonC authored May 9, 2024
1 parent 5ba8311 commit b2bc497
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 10 deletions.
4 changes: 2 additions & 2 deletions android/festago/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ android {
applicationId = "com.festago.festago"
minSdk = 28
targetSdk = 34
versionCode = 10
versionName = "2.0.1"
versionCode = 11
versionName = "2.0.2"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ class DefaultUserRepository @Inject constructor(
)
}.onSuccessOrCatch {
kakaoAuthorization.signOut()
tokenDataSource.accessToken = null
tokenDataSource.refreshToken = null
clearToken()
}
}

Expand All @@ -98,16 +97,18 @@ class DefaultUserRepository @Inject constructor(
)
}.onSuccessOrCatch {
kakaoAuthorization.deleteAccount()
tokenDataSource.accessToken = null
tokenDataSource.refreshToken = null
clearToken()
}
}

private suspend fun refresh(refreshToken: Token): Result<Unit> {
return runCatchingResponse {
authRetrofitService.refresh(RefreshRequest(refreshToken.token))
val refreshRequest = RefreshRequest(refreshToken.token)
clearToken()
authRetrofitService.refresh(refreshRequest)
}.onSuccessOrCatch { refreshTokenResponse ->
tokenDataSource.accessToken = refreshTokenResponse.accessToken.toEntity()
tokenDataSource.refreshToken = refreshTokenResponse.refreshToken.toEntity()
}
}

Expand All @@ -116,6 +117,11 @@ class DefaultUserRepository @Inject constructor(
?: Result.failure(NullPointerException("User info is null"))
}

override suspend fun clearToken() {
tokenDataSource.accessToken = null
tokenDataSource.refreshToken = null
}

companion object {
private const val SOCIAL_TYPE = "KAKAO"
private const val AUTH_PREF = "auth_pref"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,6 @@ class FakeUserRepository @Inject constructor() : UserRepository {
override suspend fun getUserInfo(): Result<UserInfo> {
return Result.success(UserInfo("", ""))
}

override suspend fun clearToken() = Unit
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ class AuthInterceptor(private val userRepository: UserRepository) : Interceptor

override fun intercept(chain: Interceptor.Chain): Response {
return runBlocking {
chain.proceed(request = getNewRequest(chain))
chain.proceed(request = getNewRequest(chain)).also {
if (it.code == 401)
userRepository.clearToken()
}
}
}

Expand All @@ -22,8 +25,7 @@ class AuthInterceptor(private val userRepository: UserRepository) : Interceptor
AUTHORIZATION_TOKEN_FORMAT.format(
userRepository.getAccessToken().getOrNull()?.token ?: "TokenIsNull",
),
)
.build()
).build()

companion object {
private const val HEADER_AUTHORIZATION = "Authorization"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ interface UserRepository {
suspend fun deleteAccount(): Result<Unit>
suspend fun rejectSignIn()
suspend fun getUserInfo(): Result<UserInfo>
suspend fun clearToken()
}

0 comments on commit b2bc497

Please sign in to comment.