diff --git a/android/app/src/main/java/by/eapp/musicroom/data/login/JwtTokenStorage.kt b/android/app/src/main/java/by/eapp/musicroom/data/login/JwtTokenStorage.kt new file mode 100644 index 0000000..f38faad --- /dev/null +++ b/android/app/src/main/java/by/eapp/musicroom/data/login/JwtTokenStorage.kt @@ -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, +) : 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") + } +} \ No newline at end of file diff --git a/android/app/src/main/java/by/eapp/musicroom/domain/repo/login/JwtTokenManager.kt b/android/app/src/main/java/by/eapp/musicroom/domain/repo/login/JwtTokenManager.kt new file mode 100644 index 0000000..1e27b57 --- /dev/null +++ b/android/app/src/main/java/by/eapp/musicroom/domain/repo/login/JwtTokenManager.kt @@ -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() +} \ No newline at end of file