Skip to content

Commit

Permalink
fix navigation, auth state and encrypt user prefs
Browse files Browse the repository at this point in the history
  • Loading branch information
stslex committed Nov 2, 2024
1 parent b788220 commit 04fda6d
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ fun InitialApp(
LaunchedEffect(Unit) {
userStore.isAuthFlow.collect { isAuth ->
val currentScreen = navHostController.currentDestination
val authScreen = Screen.Auth::class.simpleName.orEmpty()
if (
isAuth.not() &&
currentScreen != null &&
currentScreen.route != Screen.Auth.serializer().toString()
currentScreen?.route?.contains(authScreen) == true
) {
navHostController.navigate(Screen.Auth)
}
Expand Down
3 changes: 3 additions & 0 deletions core/database/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@ kotlin {
implementation(project(":core:core"))
implementation(libs.multiplatformSettings)
}
androidMain.dependencies {
implementation(libs.androidx.security.crypto)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.stslex.wizard.core.database.di

import android.content.Context
import android.content.SharedPreferences
import androidx.security.crypto.EncryptedSharedPreferences
import androidx.security.crypto.MasterKey

internal fun createEncriptedSharedPreferences(
context: Context,
name: String
): SharedPreferences {
val masterKey: MasterKey = MasterKey.Builder(context)
.setKeyScheme(MasterKey.KeyScheme.AES256_GCM)
.build()
return EncryptedSharedPreferences.create(
context,
"${name.lowercase()}_shared_prefs",
masterKey,
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
)
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.stslex.wizard.core.database.di

import android.content.Context
import com.russhwolf.settings.SharedPreferencesSettings
import com.stslex.wizard.core.database.store.UserSettings
import com.stslex.wizard.core.database.store.UserSettingsImpl
Expand All @@ -9,8 +8,10 @@ import org.koin.core.module.Module

actual fun Module.declareUserSettingsStore() {
single<UserSettings> {
val delegate = androidContext()
.getSharedPreferences(UserSettings.NAME, Context.MODE_PRIVATE)
val delegate = createEncriptedSharedPreferences(
context = androidContext(),
name = UserSettings.NAME
)
val prefs = SharedPreferencesSettings(delegate)
UserSettingsImpl(prefs)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package com.stslex.wizard.core.network.utils.token
import kotlinx.coroutines.flow.StateFlow

interface AuthController {

val isAuth: Boolean
val isAuthFlow: StateFlow<Boolean>
val accessToken: String
val refreshToken: String

suspend fun update(token: TokenModel)
suspend fun logOut()
suspend fun logout()
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.stslex.wizard.core.network.utils.token

import com.stslex.wizard.core.core.Logger
import com.stslex.wizard.core.database.store.UserStore
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
Expand All @@ -25,15 +26,21 @@ class AuthControllerImpl(
get() = userStore.refreshToken

override suspend fun update(token: TokenModel) {
Logger.d("update: $token", TAG)
userStore.accessToken = token.accessToken
userStore.refreshToken = token.refreshToken
userStore.username = token.username
userStore.uuid = token.uuid
_isAuthFlow.emit(token.accessToken.isNotEmpty())
}

override suspend fun logOut() {
override suspend fun logout() {
Logger.d("logout", TAG)
userStore.clear()
_isAuthFlow.emit(false)
}

companion object {
private const val TAG = "AuthController"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import com.stslex.wizard.core.network.utils.currentTimeMs
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

Expand Down Expand Up @@ -47,7 +46,7 @@ fun onClickDelay(
val clicker = remember(onClick) { Clicker(throttle) }
return remember(onClick) {
{
coroutineScope.launch(Dispatchers.Default) {
coroutineScope.launch {
delay(delay)
clicker.click(onClick = onClick)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ class MatchInteractorImpl(
}

override suspend fun logout() {
authController.logOut()
authController.logout()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ class ProfileInteractorImpl(
): Flow<ProfileDomainModel> = repository.getProfile(uuid).map { it.toDomain() }

override suspend fun logOut() {
authController.logOut()
authController.logout()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ class SettingsInteractorImpl(
) : SettingsInteractor {

override suspend fun logOut() {
authController.logOut()
authController.logout()
}
}
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ compose-plugin = "1.7.0"

minSdk = "24"
compileSdk = "34"
securityCrypto = "1.1.0-alpha06"
targetSdk = "34"
versionName = "1.0"
versionCode = "1"
Expand Down Expand Up @@ -40,6 +41,7 @@ serialization = "1.7.1"
buildConfig = "4.2.0"
[libraries]
android-gradlePlugin = { module = "com.android.tools.build:gradle", version.ref = "agp" }
androidx-security-crypto = { module = "androidx.security:security-crypto", version.ref = "securityCrypto" }
kotlin-gradlePlugin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin" }
compose-gradlePlugin = { module = "org.jetbrains.compose:org.jetbrains.compose.gradle.plugin", version.ref = "compose-plugin" }
ksp-gradlePlugin = { group = "com.google.devtools.ksp", name = "com.google.devtools.ksp.gradle.plugin", version.ref = "ksp" }
Expand Down

0 comments on commit 04fda6d

Please sign in to comment.