From 798734f7137a602dcf9ff62f8f950e942d402939 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=ABsungmin?= Date: Wed, 6 Nov 2024 18:46:30 +0900 Subject: [PATCH] =?UTF-8?q?[feat]=20:=20#8=20InfoDataStore=20->=20TokenMan?= =?UTF-8?q?ger=20=EC=9D=B4=EB=A6=84=20=EB=B3=80=EA=B2=BD=20=EB=B0=8F=20?= =?UTF-8?q?=ED=86=A0=ED=81=B0=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../{InfoDataStore.kt => TokenManger.kt} | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) rename app/src/main/java/org/sopt/and/utils/{InfoDataStore.kt => TokenManger.kt} (64%) diff --git a/app/src/main/java/org/sopt/and/utils/InfoDataStore.kt b/app/src/main/java/org/sopt/and/utils/TokenManger.kt similarity index 64% rename from app/src/main/java/org/sopt/and/utils/InfoDataStore.kt rename to app/src/main/java/org/sopt/and/utils/TokenManger.kt index df4e32b..b43aee2 100644 --- a/app/src/main/java/org/sopt/and/utils/InfoDataStore.kt +++ b/app/src/main/java/org/sopt/and/utils/TokenManger.kt @@ -6,17 +6,36 @@ 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 InfoDataStore(private val context: Context) { +class TokenManger @Inject constructor( + @ApplicationContext private val context: Context +) { companion object { private val Context.infoDataStore: DataStore 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 + } + } + + fun getAccessToken(): Flow { + return context.infoDataStore.data.map { prefs -> + prefs[ACCESS_TOKEN_KEY] + } + } + suspend fun saveEmail(email: String) { context.infoDataStore.edit { it[EMAIL] = email