-
Notifications
You must be signed in to change notification settings - Fork 0
Feat/#43 자동 로그인 구현 #44
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
The head ref may contain hidden characters: "feat/#43-\uC790\uB3D9-\uB85C\uADF8\uC778-\uAD6C\uD604"
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b091d14
feat : 자동 로그인 및 로그아웃 구현
starshape7 c40c185
feat : api 호출에서 accessToken 만료 시 refresh로 갱신 후 다시 호출
starshape7 9704749
Merge branch 'develop' into feat/#43-자동-로그인-구현
starshape7 e848055
feat : landingScreen 구현_1
starshape7 daa3bb7
feat : SplashScreen 구현중( 미완성)
starshape7 1361a73
feat: 자동 소셜 로그인 구현
starshape7 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
61 changes: 61 additions & 0 deletions
61
core/designsystem/src/main/java/com/umcspot/spot/designsystem/component/splash.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,61 @@ | ||
| package com.umcspot.spot.designsystem.component | ||
|
|
||
| import androidx.compose.foundation.layout.size | ||
| import androidx.compose.foundation.layout.wrapContentSize | ||
| import androidx.compose.runtime.Composable | ||
| import androidx.compose.runtime.getValue | ||
| import androidx.compose.ui.Modifier | ||
| import androidx.compose.ui.graphics.Color | ||
| import androidx.compose.ui.graphics.toArgb | ||
| import androidx.compose.ui.semantics.contentDescription | ||
| import androidx.compose.ui.semantics.semantics | ||
| import androidx.compose.ui.unit.Dp | ||
| import androidx.compose.ui.unit.dp | ||
| import com.airbnb.lottie.LottieProperty | ||
| import com.airbnb.lottie.compose.LottieAnimation | ||
| import com.airbnb.lottie.compose.LottieCompositionSpec | ||
| import com.airbnb.lottie.compose.LottieConstants | ||
| import com.airbnb.lottie.compose.rememberLottieComposition | ||
| import com.airbnb.lottie.compose.rememberLottieDynamicProperties | ||
| import com.airbnb.lottie.compose.rememberLottieDynamicProperty | ||
| import com.umcspot.spot.designsystem.R | ||
| import com.umcspot.spot.ui.extension.screenWidthDp | ||
|
|
||
| @Composable | ||
| fun Splash( | ||
| modifier: Modifier = Modifier, | ||
| speed: Float = 1f, | ||
| isPlaying: Boolean = true, | ||
| iterations: Int = LottieConstants.IterateForever, | ||
| strokeColor: Color? = null, | ||
| contentDescription: String? = "로딩 중" | ||
| ) { | ||
| val composition by rememberLottieComposition( | ||
| LottieCompositionSpec.RawRes(R.raw.splash) | ||
| ) | ||
|
|
||
| val dynamicProps = if (strokeColor != null) { | ||
| rememberLottieDynamicProperties( | ||
| rememberLottieDynamicProperty( | ||
| property = LottieProperty.STROKE_COLOR, | ||
| value = strokeColor.toArgb(), | ||
| keyPath = arrayOf("**", "Stroke 1") | ||
| ) | ||
| ) | ||
| } else { | ||
| null | ||
| } | ||
|
|
||
| LottieAnimation( | ||
| composition = composition, | ||
| iterations = iterations, | ||
| isPlaying = isPlaying, | ||
| speed = speed, | ||
| dynamicProperties = dynamicProps, | ||
| modifier = modifier | ||
| .wrapContentSize() | ||
| .semantics { | ||
| if (contentDescription != null) this.contentDescription = contentDescription | ||
| } | ||
| ) | ||
| } |
Binary file not shown.
117 changes: 117 additions & 0 deletions
117
core/network/src/main/java/com/umcspot/spot/network/TokenAuthenticator.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,117 @@ | ||
| package com.umcspot.spot.network | ||
|
|
||
| import androidx.datastore.core.DataStore | ||
| import com.umcspot.spot.datastore.token.SpotTokenData | ||
| import com.umcspot.spot.datastore.userId.SpotUserIdData | ||
| import com.umcspot.spot.network.service.TokenRefreshService | ||
| import kotlinx.coroutines.flow.first | ||
| 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 spotTokenDataStore: DataStore<SpotTokenData>, | ||
| private val spotUserIdDataStore: DataStore<SpotUserIdData>, | ||
| private val tokenRefreshService: TokenRefreshService | ||
| ) : Authenticator { | ||
|
|
||
| private val refreshLock = Any() | ||
| private object RefreshAttempted | ||
|
|
||
| override fun authenticate(route: Route?, response: Response): Request? { | ||
| val refreshAttempted = response.request.tag(RefreshAttempted::class.java) != null | ||
| if (refreshAttempted) { | ||
| clearAuthData() | ||
| return null | ||
| } | ||
| if (responseCount(response) >= 2) return null | ||
| if (response.request.url.encodedPath.endsWith("/api/auth/reissue")) return null | ||
|
|
||
| val requestAccessToken = response.request.header("Authorization") | ||
| ?.removePrefix("Bearer ") | ||
| .orEmpty() | ||
| val latestAccessToken = runBlocking { | ||
| spotTokenDataStore.data.first().accessToken | ||
| } | ||
|
|
||
| if (latestAccessToken.isNotBlank() && latestAccessToken != requestAccessToken) { | ||
| return response.request.newBuilder() | ||
| .header("Authorization", "Bearer $latestAccessToken") | ||
| .tag(RefreshAttempted::class.java, RefreshAttempted) | ||
| .build() | ||
| } | ||
|
|
||
| synchronized(refreshLock) { | ||
| val tokenData = runBlocking { spotTokenDataStore.data.first() } | ||
| if (tokenData.accessToken.isNotBlank() && tokenData.accessToken != requestAccessToken) { | ||
| return response.request.newBuilder() | ||
| .header("Authorization", "Bearer ${tokenData.accessToken}") | ||
| .tag(RefreshAttempted::class.java, RefreshAttempted) | ||
| .build() | ||
| } | ||
|
|
||
| val refreshToken = tokenData.refreshToken | ||
| if (refreshToken.isBlank()) { | ||
| clearAuthData() | ||
| return null | ||
| } | ||
|
|
||
| val refreshResponse = runBlocking { | ||
| try { | ||
| tokenRefreshService.refreshTokenData(refreshToken) | ||
| } catch (e: Exception) { | ||
| clearAuthData() | ||
| null | ||
| } | ||
| } | ||
| if (refreshResponse == null) return null | ||
|
|
||
| if (!refreshResponse.isSuccess) { | ||
| clearAuthData() | ||
| return null | ||
| } | ||
|
|
||
| val result = refreshResponse.result | ||
| runBlocking { | ||
| spotTokenDataStore.updateData { current -> | ||
| current.copy( | ||
| accessToken = result.accessToken, | ||
| refreshToken = result.refreshToken | ||
| ) | ||
| } | ||
| spotUserIdDataStore.updateData { current -> | ||
| current.copy(userId = result.userId) | ||
| } | ||
| } | ||
|
|
||
| return response.request.newBuilder() | ||
| .header("Authorization", "Bearer ${result.accessToken}") | ||
| .tag(RefreshAttempted::class.java, RefreshAttempted) | ||
| .build() | ||
| } | ||
| } | ||
|
|
||
| private fun clearAuthData() { | ||
| runBlocking { | ||
| spotTokenDataStore.updateData { current -> | ||
| current.copy(accessToken = "", refreshToken = "") | ||
| } | ||
| spotUserIdDataStore.updateData { current -> | ||
| current.copy(userId = "") | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private fun responseCount(response: Response): Int { | ||
| var count = 1 | ||
| var prior = response.priorResponse | ||
| while (prior != null) { | ||
| count++ | ||
| prior = prior.priorResponse | ||
| } | ||
| return count | ||
| } | ||
| } | ||
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
14 changes: 14 additions & 0 deletions
14
core/network/src/main/java/com/umcspot/spot/network/model/TokenRefreshResponse.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,14 @@ | ||
| package com.umcspot.spot.network.model | ||
|
|
||
| import kotlinx.serialization.SerialName | ||
| import kotlinx.serialization.Serializable | ||
|
|
||
| @Serializable | ||
| data class TokenRefreshResponse( | ||
| @SerialName("id") | ||
| val userId: String, | ||
| @SerialName("accessToken") | ||
| val accessToken: String, | ||
| @SerialName("refreshToken") | ||
| val refreshToken: String | ||
| ) |
15 changes: 13 additions & 2 deletions
15
core/network/src/main/java/com/umcspot/spot/network/service/TokenRefreshService.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 |
|---|---|---|
| @@ -1,4 +1,15 @@ | ||
| package network.service | ||
| package com.umcspot.spot.network.service | ||
|
|
||
| class TokenRefreshService { | ||
|
|
||
| import com.umcspot.spot.network.model.BaseResponse | ||
| import com.umcspot.spot.network.model.TokenRefreshResponse | ||
| import retrofit2.http.Header | ||
| import retrofit2.http.POST | ||
|
|
||
| interface TokenRefreshService { | ||
|
|
||
| @POST("/api/auth/reissue") | ||
| suspend fun refreshTokenData( | ||
| @Header("refreshToken") refreshToken: String, | ||
| ): BaseResponse<TokenRefreshResponse> | ||
| } |
7 changes: 5 additions & 2 deletions
7
data/login/src/main/java/com/umcspot/spot/login/datasource/LoginDataSource.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 |
|---|---|---|
| @@ -1,10 +1,13 @@ | ||
| package com.umcspot.spot.login.datasource | ||
|
|
||
| import com.umcspot.spot.login.dto.response.TokenResponseDto | ||
| import com.umcspot.spot.model.SocialLoginType | ||
| import com.umcspot.spot.network.model.BaseResponse | ||
| import com.umcspot.spot.network.model.NullResultResponse | ||
|
|
||
| interface LoginDataSource { | ||
| suspend fun finishSocialLogin(type : String, accessToken : String): BaseResponse<TokenResponseDto> | ||
| suspend fun getCallBackToken(type : String, accessToken : String): BaseResponse<TokenResponseDto> | ||
|
|
||
| suspend fun refreshTokenData(refreshToken : String) : BaseResponse<TokenResponseDto> | ||
|
|
||
| suspend fun spotLogout(): NullResultResponse | ||
| } |
10 changes: 8 additions & 2 deletions
10
data/login/src/main/java/com/umcspot/spot/login/datasourceimpl/LoginDataSourceImpl.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 |
|---|---|---|
| @@ -1,19 +1,25 @@ | ||
| package com.umcspot.spot.login.datasourceimpl | ||
|
|
||
| import androidx.datastore.core.DataStore | ||
| import com.umcspot.spot.login.datasource.LoginDataSource | ||
| import com.umcspot.spot.login.dto.response.TokenResponseDto | ||
| import com.umcspot.spot.login.service.LoginService | ||
| import com.umcspot.spot.network.model.BaseResponse | ||
| import com.umcspot.spot.network.model.NullResultResponse | ||
| import javax.inject.Inject | ||
|
|
||
| class LoginDataSourceImpl @Inject constructor( | ||
| private val loginService: LoginService | ||
| ) : LoginDataSource { | ||
|
|
||
| override suspend fun finishSocialLogin( | ||
| override suspend fun getCallBackToken( | ||
| type: String, | ||
| accessToken: String | ||
| ): BaseResponse<TokenResponseDto> = | ||
| loginService.getCallBackToken(type, accessToken) | ||
|
|
||
| override suspend fun refreshTokenData(refreshToken: String): BaseResponse<TokenResponseDto> = | ||
| loginService.refreshTokenData(refreshToken) | ||
|
|
||
| override suspend fun spotLogout() : NullResultResponse = | ||
| loginService.spotLogout() | ||
| } |
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.
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 refreshResponse = runBlocking { try { tokenRefreshService.refreshTokenData(refreshToken) } catch (e: Exception) { + android.util.Log.e("TokenAuthenticator", "Token refresh failed", e) clearAuthData() null } }📝 Committable suggestion
🧰 Tools
🪛 detekt (1.23.8)
[warning] 65-65: The caught exception is swallowed. The original exception could be lost.
(detekt.exceptions.SwallowedException)
🤖 Prompt for AI Agents