Implement login/logout functionality
This commit is contained in:
@@ -4,13 +4,13 @@ import kotlinx.coroutines.flow.Flow
|
||||
|
||||
interface AuthRepo {
|
||||
|
||||
val isAuthorizedFlow: Flow<Boolean>
|
||||
|
||||
suspend fun authenticate(username: String, password: String)
|
||||
|
||||
suspend fun getAuthHeader(): String?
|
||||
|
||||
suspend fun requireAuthHeader(): String
|
||||
|
||||
fun authenticationStatuses(): Flow<Boolean>
|
||||
|
||||
suspend fun logout()
|
||||
}
|
||||
@@ -3,11 +3,12 @@ package gq.kirmanak.mealient.data.auth
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
interface AuthStorage {
|
||||
|
||||
val authHeaderFlow: Flow<String?>
|
||||
|
||||
suspend fun storeAuthData(authHeader: String)
|
||||
|
||||
suspend fun getAuthHeader(): String?
|
||||
|
||||
fun authHeaderObservable(): Flow<String?>
|
||||
|
||||
suspend fun clearAuthData()
|
||||
}
|
||||
@@ -15,6 +15,9 @@ class AuthRepoImpl @Inject constructor(
|
||||
private val storage: AuthStorage,
|
||||
) : AuthRepo {
|
||||
|
||||
override val isAuthorizedFlow: Flow<Boolean>
|
||||
get() = storage.authHeaderFlow.map { it != null }
|
||||
|
||||
override suspend fun authenticate(username: String, password: String) {
|
||||
Timber.v("authenticate() called with: username = $username, password = $password")
|
||||
val accessToken = dataSource.authenticate(username, password)
|
||||
@@ -27,11 +30,6 @@ class AuthRepoImpl @Inject constructor(
|
||||
override suspend fun requireAuthHeader(): String =
|
||||
checkNotNull(getAuthHeader()) { "Auth header is null when it was required" }
|
||||
|
||||
override fun authenticationStatuses(): Flow<Boolean> {
|
||||
Timber.v("authenticationStatuses() called")
|
||||
return storage.authHeaderObservable().map { it != null }
|
||||
}
|
||||
|
||||
override suspend fun logout() {
|
||||
Timber.v("logout() called")
|
||||
storage.clearAuthData()
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package gq.kirmanak.mealient.data.auth.impl
|
||||
|
||||
import androidx.datastore.preferences.core.Preferences
|
||||
import gq.kirmanak.mealient.data.auth.AuthStorage
|
||||
import gq.kirmanak.mealient.data.storage.PreferencesStorage
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
@@ -12,7 +13,10 @@ class AuthStorageImpl @Inject constructor(
|
||||
private val preferencesStorage: PreferencesStorage,
|
||||
) : AuthStorage {
|
||||
|
||||
private val authHeaderKey by preferencesStorage::authHeaderKey
|
||||
private val authHeaderKey: Preferences.Key<String>
|
||||
get() = preferencesStorage.authHeaderKey
|
||||
override val authHeaderFlow: Flow<String?>
|
||||
get() = preferencesStorage.valueUpdates(authHeaderKey)
|
||||
|
||||
override suspend fun storeAuthData(authHeader: String) {
|
||||
Timber.v("storeAuthData() called with: authHeader = $authHeader")
|
||||
@@ -26,11 +30,6 @@ class AuthStorageImpl @Inject constructor(
|
||||
return token
|
||||
}
|
||||
|
||||
override fun authHeaderObservable(): Flow<String?> {
|
||||
Timber.v("authHeaderObservable() called")
|
||||
return preferencesStorage.valueUpdates(authHeaderKey)
|
||||
}
|
||||
|
||||
override suspend fun clearAuthData() {
|
||||
Timber.v("clearAuthData() called")
|
||||
preferencesStorage.removeValues(authHeaderKey)
|
||||
|
||||
Reference in New Issue
Block a user