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