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)
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package gq.kirmanak.mealient.data.baseurl
|
||||
|
||||
import androidx.datastore.preferences.core.Preferences
|
||||
import gq.kirmanak.mealient.data.storage.PreferencesStorage
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
@@ -9,7 +10,8 @@ class BaseURLStorageImpl @Inject constructor(
|
||||
private val preferencesStorage: PreferencesStorage,
|
||||
) : BaseURLStorage {
|
||||
|
||||
private val baseUrlKey by preferencesStorage::baseUrlKey
|
||||
private val baseUrlKey: Preferences.Key<String>
|
||||
get() = preferencesStorage.baseUrlKey
|
||||
|
||||
override suspend fun getBaseURL(): String? = preferencesStorage.getValue(baseUrlKey)
|
||||
|
||||
|
||||
@@ -1,6 +1,11 @@
|
||||
package gq.kirmanak.mealient.data.disclaimer
|
||||
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
interface DisclaimerStorage {
|
||||
|
||||
val isDisclaimerAcceptedFlow: Flow<Boolean>
|
||||
|
||||
suspend fun isDisclaimerAccepted(): Boolean
|
||||
|
||||
suspend fun acceptDisclaimer()
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package gq.kirmanak.mealient.data.disclaimer
|
||||
|
||||
import androidx.datastore.preferences.core.Preferences
|
||||
import gq.kirmanak.mealient.data.storage.PreferencesStorage
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.map
|
||||
import timber.log.Timber
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
@@ -10,7 +13,10 @@ class DisclaimerStorageImpl @Inject constructor(
|
||||
private val preferencesStorage: PreferencesStorage,
|
||||
) : DisclaimerStorage {
|
||||
|
||||
private val isDisclaimerAcceptedKey by preferencesStorage::isDisclaimerAcceptedKey
|
||||
private val isDisclaimerAcceptedKey: Preferences.Key<Boolean>
|
||||
get() = preferencesStorage.isDisclaimerAcceptedKey
|
||||
override val isDisclaimerAcceptedFlow: Flow<Boolean>
|
||||
get() = preferencesStorage.valueUpdates(isDisclaimerAcceptedKey).map { it == true }
|
||||
|
||||
override suspend fun isDisclaimerAccepted(): Boolean {
|
||||
Timber.v("isDisclaimerAccepted() called")
|
||||
|
||||
Reference in New Issue
Block a user