-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#3 added JWT manager and storage for tokens(Datastore)
- Loading branch information
Showing
2 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
android/app/src/main/java/by/eapp/musicroom/data/login/JwtTokenStorage.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
android/app/src/main/java/by/eapp/musicroom/domain/repo/login/JwtTokenManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |