-
Notifications
You must be signed in to change notification settings - Fork 1
SCRUM-272 feature: implement auto login logic #46
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
Merged
gdaegeun539
merged 8 commits into
project-lyrics:develop
from
gdaegeun539:feature/SCRUM-272-auto-login
May 13, 2026
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c22631a
feat: Centralize auth token refresh
gdaegeun539 dcccc61
feat: Route startup by restored auth state
gdaegeun539 17f1b60
feat: Move auth gate to splash route
gdaegeun539 40ebe5c
fix: Surface auto-login failure codes
gdaegeun539 594e9fa
chore: add comment
gdaegeun539 2efd7cf
fix: Fail auto-login restore consistently
gdaegeun539 208020a
fix: Preserve auto-login failure dialog state
gdaegeun539 861e071
fix: Log token refresh failure causes
gdaegeun539 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 135 additions & 0 deletions
135
app/src/main/java/com/lyrics/feelin/core/data/manager/AuthTokenRefresher.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| package com.lyrics.feelin.core.data.manager | ||
|
|
||
| import com.lyrics.feelin.core.data.datasource.remote.AuthRemoteDataSource | ||
| import com.lyrics.feelin.core.domain.model.AuthToken | ||
| import com.lyrics.feelin.util.toServerErrorDto | ||
| import javax.inject.Inject | ||
| import javax.inject.Singleton | ||
| import kotlinx.coroutines.CancellationException | ||
| import kotlinx.coroutines.sync.Mutex | ||
| import kotlinx.coroutines.sync.withLock | ||
| import retrofit2.HttpException | ||
|
|
||
| class AuthTokenRefreshException( | ||
| val errorCode: String?, | ||
| cause: Throwable, | ||
| ) : Exception(cause) | ||
|
|
||
| /** | ||
| * 서버 JWT 재발급 공통 경로입니다. | ||
| * | ||
| * 이 클래스는 런타임 401 재인증에서도 사용되므로 | ||
| * 일시적인 네트워크/서버 실패만으로는 저장된 refresh token을 삭제하지 않습니다. | ||
| * 토큰 만료·무효처럼 서버가 세션 종료를 확정한 인증 에러 코드에만 토큰을 정리하고, | ||
| * 앱 시작 자동 로그인의 더 강한 실패 정책은 [AuthRepository.restoreSession]에서 조합합니다. | ||
| */ | ||
| @Singleton | ||
| class AuthTokenRefresher @Inject constructor( | ||
| private val authRemoteDataSource: AuthRemoteDataSource, | ||
| private val authManager: AuthManager | ||
| ) { | ||
| private val refreshMutex = Mutex() | ||
|
|
||
| suspend fun refreshServerToken(staleAccessToken: String? = null): Result<AuthToken> { | ||
| return refreshMutex.withLock { | ||
| authManager.initializationComplete.await() | ||
|
|
||
| val cachedAccessToken = authManager.accessToken.value | ||
| val cachedRefreshToken = authManager.refreshToken.value | ||
| val cachedUserId = authManager.userId.value | ||
|
|
||
| val cachedAuthToken = createCachedAuthToken( | ||
| staleAccessToken = staleAccessToken, | ||
| cachedAccessToken = cachedAccessToken, | ||
| cachedRefreshToken = cachedRefreshToken, | ||
| cachedUserId = cachedUserId, | ||
| ) | ||
| if (cachedAuthToken != null) { | ||
| return@withLock Result.success( | ||
| cachedAuthToken | ||
| ) | ||
| } | ||
|
|
||
| val refreshToken = cachedRefreshToken | ||
| ?: return@withLock clearTokensAndFail(IllegalStateException("Refresh token is null")) | ||
|
|
||
| authRemoteDataSource.reIssueToken(refreshToken = refreshToken) | ||
| .fold( | ||
| onSuccess = { authToken -> | ||
| authManager.saveServerToken( | ||
| accessToken = authToken.accessToken, | ||
| refreshToken = authToken.refreshToken, | ||
| userId = authToken.userId, | ||
| ) | ||
| Result.success(authToken) | ||
| }, | ||
| onFailure = { exception -> | ||
| if (exception is CancellationException) { | ||
| throw exception | ||
| } | ||
| handleRefreshFailure(exception) | ||
| }, | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| private suspend fun handleRefreshFailure(exception: Throwable): Result<AuthToken> { | ||
| val refreshException = exception.toAuthTokenRefreshException() | ||
|
|
||
| // 401 재인증 경로에서는 일시 실패 후 다음 요청에서 다시 복구할 수 있도록 토큰을 보존합니다. | ||
| return if (refreshException.errorCode in TERMINAL_AUTH_ERROR_CODES) { | ||
| clearTokensAndFail(refreshException) | ||
| } else { | ||
| Result.failure(refreshException) | ||
| } | ||
| } | ||
|
|
||
| private suspend fun clearTokensAndFail(exception: Throwable): Result<AuthToken> { | ||
| authManager.clearTokens() | ||
| return Result.failure(exception) | ||
| } | ||
|
|
||
| private fun Throwable.toAuthTokenRefreshException(): AuthTokenRefreshException { | ||
| val errorCode = if (this is HttpException) { | ||
| toServerErrorDto().errorCode | ||
| } else { | ||
| null | ||
| } | ||
|
|
||
| return AuthTokenRefreshException( | ||
| errorCode = errorCode, | ||
| cause = this, | ||
| ) | ||
| } | ||
|
|
||
| private fun createCachedAuthToken( | ||
| staleAccessToken: String?, | ||
| cachedAccessToken: String?, | ||
| cachedRefreshToken: String?, | ||
| cachedUserId: Long? | ||
| ): AuthToken? { | ||
| val staleToken = staleAccessToken ?: return null | ||
| val accessToken = cachedAccessToken ?: return null | ||
| val refreshToken = cachedRefreshToken ?: return null | ||
| val userId = cachedUserId ?: return null | ||
|
|
||
| if (accessToken == staleToken) { | ||
| return null | ||
| } | ||
|
|
||
| return AuthToken( | ||
| accessToken = accessToken, | ||
| refreshToken = refreshToken, | ||
| userId = userId, | ||
| ) | ||
| } | ||
|
|
||
| companion object { | ||
| private val TERMINAL_AUTH_ERROR_CODES = setOf( | ||
| "01001", | ||
| "01002", | ||
| "01004", | ||
| "01008", | ||
| ) | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.