Skip to content

Commit

Permalink
[feat] : #8 InfoDataStore -> TokenManger 이름 변경 및 토큰 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
1971123-seongmin committed Nov 6, 2024
1 parent 8d2e980 commit 798734f
Showing 1 changed file with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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<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
}
}

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
Expand Down

0 comments on commit 798734f

Please sign in to comment.