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:
@@ -0,0 +1,14 @@
|
||||
// shared/src/androidMain/kotlin/app/klottr/util/PlatformAuthStorage.android.kt
|
||||
package app.klottr.util
|
||||
|
||||
/** In-memory stub; Android keeps its own SharedPreferences today. */
|
||||
actual object PlatformAuthStorage {
|
||||
private val mem = mutableMapOf<String, String>()
|
||||
actual fun get(key: String) = mem[key]
|
||||
actual fun set(key: String, value: String?) {
|
||||
if (value == null) mem.remove(key) else mem[key] = value
|
||||
}
|
||||
actual fun clear() = mem.clear()
|
||||
|
||||
actual fun install(delegate: AuthStorageDelegate) { /* no-op on Android */ }
|
||||
}
|
||||
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()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
// shared/src/commonMain/kotlin/app/klottr/util/AuthStorageDelegate.kt
|
||||
package app.klottr.util
|
||||
|
||||
interface AuthStorageDelegate {
|
||||
fun get(key: String): String?
|
||||
fun set(key: String, value: String?)
|
||||
fun clear()
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
// shared/src/commonMain/kotlin/app/klottr/util/PlatformAuthStorage.kt
|
||||
package app.klottr.util
|
||||
|
||||
/** Minimal key-value used only by AuthShared. iOS uses Keychain. */
|
||||
expect object PlatformAuthStorage {
|
||||
fun get(key: String): String?
|
||||
fun set(key: String, value: String?)
|
||||
fun clear()
|
||||
|
||||
/** iOS will use this; Android can be a no-op */
|
||||
fun install(delegate: AuthStorageDelegate)
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
// shared/src/iosMain/kotlin/app/klottr/util/PlatformAuthStorage.ios.kt
|
||||
package app.klottr.util
|
||||
|
||||
actual object PlatformAuthStorage {
|
||||
private var delegate: AuthStorageDelegate? = null
|
||||
|
||||
actual fun install(delegate: AuthStorageDelegate) {
|
||||
this.delegate = delegate
|
||||
}
|
||||
|
||||
actual fun get(key: String): String? = delegate?.get(key)
|
||||
actual fun set(key: String, value: String?) { delegate?.set(key, value) }
|
||||
actual fun clear() { delegate?.clear() }
|
||||
}
|
||||
Reference in New Issue
Block a user