Skip to content

Commit

Permalink
#3 added JWT manager and storage for tokens(Datastore)
Browse files Browse the repository at this point in the history
  • Loading branch information
nastix123 committed Jul 14, 2024
1 parent 245d18d commit a928829
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package by.eapp.musicroom.data.login

import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.stringPreferencesKey
import by.eapp.musicroom.domain.repo.login.JwtTokenManager
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.map
import javax.inject.Inject

class JwtTokenStorage @Inject constructor(
private val dataStore: DataStore<Preferences>,
) : JwtTokenManager {
override suspend fun saveAccessJwt(token: String) {
dataStore.edit { preferences ->
preferences[ACCESS_JWT_KEY] = token
}
}

override suspend fun saveRefreshJwt(token: String) {
dataStore.edit { preferences ->
preferences[REFRESH_JWT_KEY] = token
}
}

override suspend fun getAccessJwt(): String? {
return dataStore.data.map { preferences ->
preferences[ACCESS_JWT_KEY]
}.first()
}

override suspend fun getRefreshJwt(): String? {
return dataStore.data.map { preferences ->
preferences[REFRESH_JWT_KEY]
}.first()
}

override suspend fun clearAllTokens() {
dataStore.edit { preferences ->
preferences.remove(ACCESS_JWT_KEY)
preferences.remove(REFRESH_JWT_KEY)
}
}

companion object {
val ACCESS_JWT_KEY = stringPreferencesKey("access_jwt")
val REFRESH_JWT_KEY = stringPreferencesKey("refresh_jwt")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package by.eapp.musicroom.domain.repo.login

interface JwtTokenManager {
suspend fun saveAccessJwt(token: String)
suspend fun saveRefreshJwt(token: String)
suspend fun getAccessJwt(): String?
suspend fun getRefreshJwt(): String?
suspend fun clearAllTokens()
}

0 comments on commit a928829

Please sign in to comment.