ios(sign-in): add Google SignIn for ios app
Add KMP bridges to synch login state Use keychain for storing login state on ios Setup interop beween KMP and ios for sign in
This commit is contained in:
54
shared/src/commonMain/kotlin/app/klottr/util/AuthShared.kt
Normal file
54
shared/src/commonMain/kotlin/app/klottr/util/AuthShared.kt
Normal file
@@ -0,0 +1,54 @@
|
||||
// shared/src/commonMain/kotlin/app/klottr/util/AuthShared.kt
|
||||
package app.klottr.util
|
||||
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlin.concurrent.Volatile
|
||||
|
||||
object AuthShared {
|
||||
private const val KEY_IDTOKEN = "id_token"
|
||||
private const val KEY_SUB = "sub"
|
||||
private const val KEY_EMAIL = "email"
|
||||
|
||||
@Volatile var idToken: String? = null; private set
|
||||
@Volatile var userId: String? = null; private set
|
||||
@Volatile
|
||||
var email: String? = null; private set
|
||||
|
||||
private val _loggedIn = MutableStateFlow(false)
|
||||
val loggedIn: StateFlow<Boolean> = _loggedIn
|
||||
|
||||
private val _email = MutableStateFlow<String?>(null)
|
||||
val emailFlow: StateFlow<String?> = _email
|
||||
|
||||
/** Called by platform code after a successful sign-in (or silent restore). */
|
||||
fun applyLogin(token: String?, sub: String?, mail: String?) {
|
||||
idToken = token
|
||||
userId = sub
|
||||
email = mail
|
||||
_loggedIn.value = token != null
|
||||
_email.value = mail
|
||||
if (token != null) {
|
||||
PlatformAuthStorage.set(KEY_IDTOKEN, token)
|
||||
PlatformAuthStorage.set(KEY_SUB, sub)
|
||||
PlatformAuthStorage.set(KEY_EMAIL, mail)
|
||||
}
|
||||
}
|
||||
|
||||
/** Load last known auth state from platform storage (Keychain on iOS). */
|
||||
fun loadFromStorage() {
|
||||
idToken = PlatformAuthStorage.get(KEY_IDTOKEN)
|
||||
userId = PlatformAuthStorage.get(KEY_SUB)
|
||||
email = PlatformAuthStorage.get(KEY_EMAIL)
|
||||
_loggedIn.value = idToken != null
|
||||
_email.value = email
|
||||
}
|
||||
|
||||
/** Clear both in-memory state and platform storage. */
|
||||
fun clearAll() {
|
||||
idToken = null; userId = null; email = null
|
||||
_loggedIn.value = false
|
||||
_email.value = null
|
||||
PlatformAuthStorage.clear()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user