Skip to content
Snippets Groups Projects
Commit 0923004e authored by Jonas Broeckmann's avatar Jonas Broeckmann
Browse files

Fix credentials manager weirdness

parent 4c407c5f
No related branches found
No related tags found
No related merge requests found
......@@ -126,6 +126,7 @@ class App : Application(), RepositoryProvider, CoroutineScope by MainScope() + C
hiwiTrackerRepository
)
override val loginRepository = LoginRepository(
coroutineScope = this,
credentialsRepository = credentials,
settings = settingsRepository,
portal = portalRepository,
......
......@@ -11,12 +11,12 @@ import androidx.credentials.exceptions.CreateCredentialException
import androidx.credentials.exceptions.CreateCredentialUnsupportedException
import androidx.credentials.exceptions.GetCredentialCancellationException
import androidx.credentials.exceptions.GetCredentialException
import net.novagamestudios.common_utils.core.toastShort
import net.novagamestudios.common_utils.core.logging.Logger
import net.novagamestudios.common_utils.core.logging.debug
import net.novagamestudios.common_utils.core.logging.error
import net.novagamestudios.common_utils.core.logging.info
import net.novagamestudios.common_utils.core.logging.verbose
import net.novagamestudios.common_utils.core.toastShort
import net.novagamestudios.kaffeekasse.BuildConfig
import net.novagamestudios.kaffeekasse.model.credentials.DeviceCredentials
import net.novagamestudios.kaffeekasse.model.credentials.Login
......@@ -122,7 +122,7 @@ class Credentials(
}
companion object {
private const val EnableDebugUserCredentials = true
private const val EnableDebugDeviceCredentials = true
private const val EnableDebugUserCredentials = false
private const val EnableDebugDeviceCredentials = false
}
}
\ No newline at end of file
package net.novagamestudios.kaffeekasse.repositories
import android.content.Context
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.async
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.sync.Mutex
......@@ -23,6 +25,7 @@ import net.novagamestudios.kaffeekasse.repositories.i11.KaffeekasseRepository
import net.novagamestudios.kaffeekasse.repositories.i11.PortalRepository
class LoginRepository(
private val coroutineScope: CoroutineScope,
private val credentialsRepository: Credentials,
private val settings: SettingsRepository,
private val portal: PortalRepository,
......@@ -45,13 +48,16 @@ class LoginRepository(
}
}
private suspend fun <R> scopedToApp(block: suspend CoroutineScope.() -> R): R = coroutineScope.async(block = block).await()
private suspend fun <R> actionScopedToApp(block: suspend CoroutineScope.() -> R) = action { scopedToApp(block) }
private val _errors = MutableStateFlow<List<Exception>?>(null)
val errors = _errors.asStateFlow()
suspend fun forceUpdateSession() = action {
suspend fun forceUpdateSession() = actionScopedToApp {
portal.forceUpdateSession()
}
......@@ -59,7 +65,7 @@ class LoginRepository(
val autoLogin by lazy { settings.values.mapState { it.autoLogin } }
private var autoLoginAttemptAvailable = true
private suspend fun performUserLogin(login: Login): Boolean = action {
private suspend fun performUserLogin(login: Login): Boolean {
_errors.value = null
info { "Logging in user: ${login.username}" }
val success = try {
......@@ -72,7 +78,7 @@ class LoginRepository(
false
}
autoLoginAttemptAvailable = success
return@action success
return success
}
suspend fun tryAutoLogin(activityContext: Context): Login? = action {
......@@ -98,15 +104,17 @@ class LoginRepository(
}
}
autoLoginAttemptAvailable = false
info { "Trying to auto login user: ${login.username}" }
performUserLogin(login)
credentialsRepository.storeSessionData(portal.session.value.data)
scopedToApp {
info { "Trying to auto login user: ${login.username}" }
performUserLogin(login)
credentialsRepository.storeSessionData(portal.session.value.data)
}
return@action login
}
suspend fun login(login: Login, autoLogin: Boolean, activityContext: Context) = action {
suspend fun login(login: Login, autoLogin: Boolean, activityContext: Context) = actionScopedToApp {
val cleanedLogin = login.copy(username = login.username.trim())
if (!performUserLogin(cleanedLogin)) return@action
if (!performUserLogin(cleanedLogin)) return@actionScopedToApp
with(activityContext) {
updateAutoLogin(cleanedLogin, autoLogin)
}
......@@ -140,7 +148,7 @@ class LoginRepository(
suspend fun login(user: BasicUserInfo, auth: UserAuthCredentials = UserAuthCredentials.Empty): LoginResult = action {
suspend fun login(user: BasicUserInfo, auth: UserAuthCredentials = UserAuthCredentials.Empty): LoginResult = actionScopedToApp {
try {
when (portal.loginUser(
user.id,
......@@ -165,18 +173,18 @@ class LoginRepository(
}
suspend fun logoutUser() = action {
suspend fun logoutUser() = actionScopedToApp {
credentialsRepository.storeSessionData(null)
portal.logoutUser()
}
private suspend fun performDeviceLogin(credentials: DeviceCredentials): Boolean = action {
private suspend fun performDeviceLogin(credentials: DeviceCredentials): Boolean {
info { "Logging in device: ${credentials.deviceId}" }
val success = kaffeekasse.loginDevice(credentials)
debug { "Device logged in" }
return@action success
return success
}
suspend fun tryAutoLoginDevice(): Unit = action {
......@@ -185,20 +193,22 @@ class LoginRepository(
val credentials = credentialsRepository.deviceCredentials()
?.takeIf { it.isValid }
?: return@action
performDeviceLogin(credentials)
scopedToApp {
performDeviceLogin(credentials)
}
}
suspend fun loginDevice(credentials: DeviceCredentials): Unit = action {
if (!credentials.isValid) return@action
suspend fun loginDevice(credentials: DeviceCredentials): Unit = actionScopedToApp {
if (!credentials.isValid) return@actionScopedToApp
val cleanedCredentials = credentials.copy(
deviceId = credentials.deviceId.trim(),
apiKey = credentials.apiKey.trim()
)
if (!performDeviceLogin(cleanedCredentials)) return@action
if (!performDeviceLogin(cleanedCredentials)) return@actionScopedToApp
credentialsRepository.storeDeviceCredentials(cleanedCredentials)
}
suspend fun logoutDevice(): Unit = action {
suspend fun logoutDevice(): Unit = actionScopedToApp {
credentialsRepository.storeDeviceCredentials(null)
kaffeekasse.logoutDevice()
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment