generated from AND-SOPT-ANDROID/and-sopt-android-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feat] : #8 로그인 응답으로 받는 토큰 DataStore에 저장
- Loading branch information
1 parent
f6be2c0
commit b60260f
Showing
6 changed files
with
31 additions
and
40 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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
32 changes: 3 additions & 29 deletions
32
...in/java/org/sopt/and/utils/TokenManger.kt → ...n/java/org/sopt/and/utils/TokenManager.kt
This file contains 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,63 +1,37 @@ | ||
package org.sopt.and.utils | ||
|
||
import android.content.Context | ||
import android.util.Log | ||
import androidx.datastore.core.DataStore | ||
import androidx.datastore.preferences.core.Preferences | ||
import androidx.datastore.preferences.core.edit | ||
import androidx.datastore.preferences.core.stringPreferencesKey | ||
import androidx.datastore.preferences.preferencesDataStore | ||
import dagger.hilt.android.qualifiers.ApplicationContext | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.firstOrNull | ||
import kotlinx.coroutines.flow.map | ||
import kotlinx.coroutines.runBlocking | ||
import javax.inject.Inject | ||
|
||
class TokenManger @Inject constructor( | ||
class TokenManager @Inject constructor( | ||
@ApplicationContext private val context: Context | ||
) { | ||
|
||
companion object { | ||
private val Context.infoDataStore: DataStore<Preferences> by preferencesDataStore(name = "info") | ||
private val ACCESS_TOKEN_KEY = stringPreferencesKey("access_token") | ||
val EMAIL = stringPreferencesKey("user_email") | ||
val PWD = stringPreferencesKey("user_pwd") | ||
} | ||
|
||
fun saveToken(token: String) = runBlocking { | ||
context.infoDataStore.edit { prefs -> | ||
prefs[ACCESS_TOKEN_KEY] = token | ||
} | ||
Log.d("토큰", "토큰 : $token") | ||
} | ||
|
||
fun getAccessToken(): Flow<String?> { | ||
return context.infoDataStore.data.map { prefs -> | ||
prefs[ACCESS_TOKEN_KEY] | ||
} | ||
} | ||
|
||
suspend fun saveEmail(email: String) { | ||
context.infoDataStore.edit { | ||
it[EMAIL] = email | ||
} | ||
} | ||
|
||
suspend fun savePwd(pwd: String) { | ||
context.infoDataStore.edit { | ||
it[PWD] = pwd | ||
} | ||
} | ||
|
||
fun getEmail(): Flow<String?> { | ||
return context.infoDataStore.data.map { prefs -> | ||
prefs[EMAIL] | ||
} | ||
} | ||
|
||
fun getPwd(): Flow<String?> { | ||
return context.infoDataStore.data.map { prefs -> | ||
prefs[PWD] | ||
} | ||
} | ||
|
||
} |