-
Notifications
You must be signed in to change notification settings - Fork 4
[CHORE,FEAT] 플랫폼별 TokenManager 구현, Ktor client에서의 토큰 처리 구현 #21
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
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
76feab5
[CHORE]: ktor auth 기본 틀 작성
casper-jr 9ba6da7
[CHORE]: ktor auth 의존성 추가
casper-jr bebb73f
[CHORE]: 토큰 재발급 로직 작성
casper-jr 3b1620a
[CHORE]: datastore 의존성 추가
casper-jr 6140fe2
[FEAT]: TokenManager 인터페이스 구현
casper-jr 6d2bd76
[FEAT]: 플랫폼별 datastore 생성 로직 작성
casper-jr af487cb
[FEAT]: 실제 의존성 주입에 사용될 구현체 작성
casper-jr 95c2bfb
[FEAT]: 각 플랫폼별 TokenManager 의존성 주입
casper-jr 375060b
[FEAT]: 기존 ktor client에 TokenManager 의존성 주입
casper-jr b3034d8
[FEAT]: TokenManager 테스트를 위한 Screen, ViewModel 생성
casper-jr 150eaa6
[CHORE]: DI에 테스트 viewmodel 추가, 테스트 화면 실행
casper-jr 8007fa5
[FEAT]: ktor client에서 token 저장, 불러오기 구현
casper-jr ea1d6ed
[FEAT]: Authorization 헤더가 필요없는 path들에 대한 예외처리
casper-jr 24a4772
[FIX]: gradle 의존성 수정
casper-jr 1c12fb1
Merge branch 'develop' into feat/ktorauth
casper-jr 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
13 changes: 13 additions & 0 deletions
13
composeApp/src/androidMain/kotlin/org/whosin/client/datastore/createDataStore.android.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,13 @@ | ||
| package org.whosin.client.datastore | ||
|
|
||
| import android.content.Context | ||
| import androidx.datastore.core.DataStore | ||
| import androidx.datastore.preferences.core.Preferences | ||
| import org.whosin.client.core.datastore.DATA_STORE_FILE_NAME | ||
| import org.whosin.client.core.datastore.createDataStore | ||
|
|
||
| fun createDataStore(context: Context): DataStore<Preferences> { | ||
| return createDataStore { | ||
| context.filesDir.resolve(DATA_STORE_FILE_NAME).absolutePath | ||
| } | ||
| } |
8 changes: 8 additions & 0 deletions
8
composeApp/src/androidMain/kotlin/org/whosin/client/di/DIModules.android.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,11 +1,19 @@ | ||
| package org.whosin.client.di | ||
|
|
||
| import androidx.datastore.core.DataStore | ||
| import androidx.datastore.preferences.core.Preferences | ||
| import io.ktor.client.engine.HttpClientEngine | ||
| import io.ktor.client.engine.okhttp.OkHttp | ||
| import org.koin.android.ext.koin.androidContext | ||
| import org.koin.core.module.Module | ||
| import org.koin.dsl.module | ||
| import org.whosin.client.core.datastore.TokenManager | ||
| import org.whosin.client.core.datastore.TokenManagerImpl | ||
| import org.whosin.client.datastore.createDataStore | ||
|
|
||
| actual val platformModule: Module | ||
| get() = module { | ||
| single<HttpClientEngine> { OkHttp.create() } | ||
| single<DataStore<Preferences>> { createDataStore(androidContext())} | ||
| single<TokenManager> { TokenManagerImpl(get()) } | ||
| } |
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
8 changes: 8 additions & 0 deletions
8
composeApp/src/commonMain/kotlin/org/whosin/client/core/datastore/TokenManager.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,8 @@ | ||
| package org.whosin.client.core.datastore | ||
|
|
||
| interface TokenManager { | ||
| suspend fun getAccessToken(): String? | ||
| suspend fun getRefreshToken(): String? | ||
| suspend fun saveTokens(accessToken: String, refreshToken: String) | ||
| suspend fun clearToken() | ||
| } |
28 changes: 28 additions & 0 deletions
28
composeApp/src/commonMain/kotlin/org/whosin/client/core/datastore/TokenManagerImpl.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,28 @@ | ||
| package org.whosin.client.core.datastore | ||
|
|
||
| import androidx.datastore.core.DataStore | ||
| import androidx.datastore.preferences.core.Preferences | ||
| import androidx.datastore.preferences.core.edit | ||
| import androidx.datastore.preferences.core.stringPreferencesKey | ||
| import kotlinx.coroutines.flow.first | ||
|
|
||
| class TokenManagerImpl( | ||
| private val dataStore: DataStore<Preferences> | ||
| ): TokenManager { | ||
| private val accessKey = stringPreferencesKey("access_token") | ||
| private val refreshKey = stringPreferencesKey("refresh_token") | ||
|
|
||
| override suspend fun getAccessToken(): String? = dataStore.data.first()[accessKey] | ||
| override suspend fun getRefreshToken(): String? = dataStore.data.first()[refreshKey] | ||
|
|
||
| override suspend fun saveTokens(accessToken: String, refreshToken: String) { | ||
| dataStore.edit { | ||
| it[accessKey] = accessToken | ||
| it[refreshKey] = refreshToken | ||
| } | ||
| } | ||
|
|
||
| override suspend fun clearToken() { | ||
| dataStore.edit { it.clear() } | ||
| } | ||
| } | ||
14 changes: 14 additions & 0 deletions
14
composeApp/src/commonMain/kotlin/org/whosin/client/core/datastore/createDataStore.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 org.whosin.client.core.datastore | ||
|
|
||
| import androidx.datastore.core.DataStore | ||
| import androidx.datastore.preferences.core.PreferenceDataStoreFactory | ||
| import androidx.datastore.preferences.core.Preferences | ||
| import okio.Path.Companion.toPath | ||
|
|
||
| fun createDataStore(producePath: () -> String): DataStore<Preferences> { | ||
| return PreferenceDataStoreFactory.createWithPath( | ||
| produceFile = { producePath().toPath() } | ||
| ) | ||
| } | ||
|
|
||
| internal const val DATA_STORE_FILE_NAME = "prefs.preferences_pb" |
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
10 changes: 10 additions & 0 deletions
10
...oseApp/src/commonMain/kotlin/org/whosin/client/data/dto/request/ReissueTokenRequestDto.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,10 @@ | ||
| package org.whosin.client.data.dto.request | ||
|
|
||
| import kotlinx.serialization.SerialName | ||
| import kotlinx.serialization.Serializable | ||
|
|
||
| @Serializable | ||
| data class ReissueTokenRequestDto( | ||
| @SerialName("refreshToken") | ||
| val refreshToken: String | ||
| ) |
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
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.
DataStore 읽기 실패 시 앱이 크래시됩니다
현재
dataStore.data.first()호출에서 발생하는IOException을 잡아주지 않아 복구 불가능한 크래시로 이어집니다. 공식 가이드처럼emptyPreferences()를 emit하도록 잡아주세요.📝 Committable suggestion
🤖 Prompt for AI Agents