From 03a547f896f990e31f6089edf344f4f19b3317fe Mon Sep 17 00:00:00 2001 From: Olof Hedman Date: Wed, 10 Sep 2025 16:55:41 +0200 Subject: [PATCH] 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 --- app/src/main/java/app/klottr/util/Auth.kt | 7 ++ ios/Klottr/Klottr.xcodeproj/project.pbxproj | 61 +++++++++++++++- .../xcshareddata/swiftpm/Package.resolved | 69 +++++++++++++++++++ ios/Klottr/Klottr/AppDelegate.swift | 26 +++++++ ios/Klottr/Klottr/ContentView.swift | 10 +++ ios/Klottr/Klottr/GoogleAuthError.swift | 63 +++++++++++++++++ ios/Klottr/Klottr/Info.plist | 32 +++++++++ ios/Klottr/Klottr/KeychainStorage.swift | 57 +++++++++++++++ ios/Klottr/Klottr/KlottrApp.swift | 3 + .../util/PlatformAuthStorage.android.kt | 14 ++++ .../kotlin/app/klottr/util/AuthShared.kt | 54 +++++++++++++++ .../app/klottr/util/AuthStorageDelegate.kt | 8 +++ .../app/klottr/util/PlatformAuthStorage.kt | 13 ++++ .../klottr/util/PlatformAuthStorage.ios.kt | 14 ++++ 14 files changed, 429 insertions(+), 2 deletions(-) create mode 100644 ios/Klottr/Klottr.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved create mode 100644 ios/Klottr/Klottr/AppDelegate.swift create mode 100644 ios/Klottr/Klottr/GoogleAuthError.swift create mode 100644 ios/Klottr/Klottr/Info.plist create mode 100644 ios/Klottr/Klottr/KeychainStorage.swift create mode 100644 shared/src/androidMain/kotlin/app/klottr/util/PlatformAuthStorage.android.kt create mode 100644 shared/src/commonMain/kotlin/app/klottr/util/AuthShared.kt create mode 100644 shared/src/commonMain/kotlin/app/klottr/util/AuthStorageDelegate.kt create mode 100644 shared/src/commonMain/kotlin/app/klottr/util/PlatformAuthStorage.kt create mode 100644 shared/src/iosMain/kotlin/app/klottr/util/PlatformAuthStorage.ios.kt diff --git a/app/src/main/java/app/klottr/util/Auth.kt b/app/src/main/java/app/klottr/util/Auth.kt index 35f49b0..564a377 100644 --- a/app/src/main/java/app/klottr/util/Auth.kt +++ b/app/src/main/java/app/klottr/util/Auth.kt @@ -9,6 +9,7 @@ import com.google.android.gms.auth.api.signin.GoogleSignIn import com.google.android.gms.auth.api.signin.GoogleSignInAccount import com.google.android.gms.auth.api.signin.GoogleSignInClient import com.google.android.gms.auth.api.signin.GoogleSignInOptions +import app.klottr.util.AuthShared object Auth { @@ -50,6 +51,8 @@ object Auth { email = sp.getString(KEY_EMAIL, null) _loggedIn.value = idToken != null _email.value = email + // Keep shared KMP state in sync (for iOS/Compose common UI) + AuthShared.applyLogin(idToken, userId, email) } /** Spara kontoinfo efter lyckad inloggning och uppdatera flöden. */ @@ -68,6 +71,8 @@ object Auth { } _loggedIn.value = token != null _email.value = mail + // Sync shared KMP state + AuthShared.applyLogin(token, sub, mail) if (token == null) { Log.w("Klottr/Auth", "ID‑token är null efter sign‑in. Kontrollera WEB_CLIENT_ID i Auth.kt och på servern.") } @@ -82,6 +87,8 @@ object Auth { signInClient(ctx).signOut() _loggedIn.value = false _email.value = null + // Clear shared KMP state too + AuthShared.clearAll() } /** diff --git a/ios/Klottr/Klottr.xcodeproj/project.pbxproj b/ios/Klottr/Klottr.xcodeproj/project.pbxproj index be2db13..a9d4c89 100644 --- a/ios/Klottr/Klottr.xcodeproj/project.pbxproj +++ b/ios/Klottr/Klottr.xcodeproj/project.pbxproj @@ -6,6 +6,11 @@ objectVersion = 77; objects = { +/* Begin PBXBuildFile section */ + 412CF4A32E719E7D00EADB15 /* shared.xcframework in Frameworks */ = {isa = PBXBuildFile; fileRef = 412CF4A22E719E7D00EADB15 /* shared.xcframework */; }; + 412CF4B12E71B7C300EADB15 /* GoogleSignIn in Frameworks */ = {isa = PBXBuildFile; productRef = 412CF4B02E71B7C300EADB15 /* GoogleSignIn */; }; +/* End PBXBuildFile section */ + /* Begin PBXContainerItemProxy section */ 412CF4592E7037D800EADB15 /* PBXContainerItemProxy */ = { isa = PBXContainerItemProxy; @@ -27,11 +32,25 @@ 412CF4482E7037D700EADB15 /* Klottr.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Klottr.app; sourceTree = BUILT_PRODUCTS_DIR; }; 412CF4582E7037D800EADB15 /* KlottrTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = KlottrTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 412CF4622E7037D800EADB15 /* KlottrUITests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = KlottrUITests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; + 412CF4A22E719E7D00EADB15 /* shared.xcframework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcframework; name = shared.xcframework; path = "/Users/olle/Documents/git-personal/klottr/shared/build/XCFrameworks/release/shared.xcframework"; sourceTree = ""; }; /* End PBXFileReference section */ +/* Begin PBXFileSystemSynchronizedBuildFileExceptionSet section */ + 412CF4B42E71C14D00EADB15 /* Exceptions for "Klottr" folder in "Klottr" target */ = { + isa = PBXFileSystemSynchronizedBuildFileExceptionSet; + membershipExceptions = ( + Info.plist, + ); + target = 412CF4472E7037D700EADB15 /* Klottr */; + }; +/* End PBXFileSystemSynchronizedBuildFileExceptionSet section */ + /* Begin PBXFileSystemSynchronizedRootGroup section */ 412CF44A2E7037D700EADB15 /* Klottr */ = { isa = PBXFileSystemSynchronizedRootGroup; + exceptions = ( + 412CF4B42E71C14D00EADB15 /* Exceptions for "Klottr" folder in "Klottr" target */, + ); path = Klottr; sourceTree = ""; }; @@ -52,6 +71,8 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( + 412CF4A32E719E7D00EADB15 /* shared.xcframework in Frameworks */, + 412CF4B12E71B7C300EADB15 /* GoogleSignIn in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -75,9 +96,11 @@ 412CF43F2E7037D600EADB15 = { isa = PBXGroup; children = ( + 412CF4A22E719E7D00EADB15 /* shared.xcframework */, 412CF44A2E7037D700EADB15 /* Klottr */, 412CF45B2E7037D800EADB15 /* KlottrTests */, 412CF4652E7037D800EADB15 /* KlottrUITests */, + 412CF4AF2E71B7C300EADB15 /* Frameworks */, 412CF4492E7037D700EADB15 /* Products */, ); sourceTree = ""; @@ -92,6 +115,13 @@ name = Products; sourceTree = ""; }; + 412CF4AF2E71B7C300EADB15 /* Frameworks */ = { + isa = PBXGroup; + children = ( + ); + name = Frameworks; + sourceTree = ""; + }; /* End PBXGroup section */ /* Begin PBXNativeTarget section */ @@ -112,6 +142,7 @@ ); name = Klottr; packageProductDependencies = ( + 412CF4B02E71B7C300EADB15 /* GoogleSignIn */, ); productName = Klottr; productReference = 412CF4482E7037D700EADB15 /* Klottr.app */; @@ -195,6 +226,9 @@ ); mainGroup = 412CF43F2E7037D600EADB15; minimizedProjectReferenceProxies = 1; + packageReferences = ( + 412CF4A62E71AF4C00EADB15 /* XCRemoteSwiftPackageReference "GoogleSignIn-iOS" */, + ); preferredProjectObjectVersion = 77; productRefGroup = 412CF4492E7037D700EADB15 /* Products */; projectDirPath = ""; @@ -398,7 +432,9 @@ DEVELOPMENT_ASSET_PATHS = "\"Klottr/Preview Content\""; DEVELOPMENT_TEAM = 9LY47TPGT7; ENABLE_PREVIEWS = YES; - GENERATE_INFOPLIST_FILE = YES; + FRAMEWORK_SEARCH_PATHS = ""; + GENERATE_INFOPLIST_FILE = NO; + INFOPLIST_FILE = Klottr/Info.plist; INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES; INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES; INFOPLIST_KEY_UILaunchScreen_Generation = YES; @@ -427,7 +463,9 @@ DEVELOPMENT_ASSET_PATHS = "\"Klottr/Preview Content\""; DEVELOPMENT_TEAM = 9LY47TPGT7; ENABLE_PREVIEWS = YES; - GENERATE_INFOPLIST_FILE = YES; + FRAMEWORK_SEARCH_PATHS = ""; + GENERATE_INFOPLIST_FILE = NO; + INFOPLIST_FILE = Klottr/Info.plist; INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES; INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES; INFOPLIST_KEY_UILaunchScreen_Generation = YES; @@ -558,6 +596,25 @@ defaultConfigurationName = Release; }; /* End XCConfigurationList section */ + +/* Begin XCRemoteSwiftPackageReference section */ + 412CF4A62E71AF4C00EADB15 /* XCRemoteSwiftPackageReference "GoogleSignIn-iOS" */ = { + isa = XCRemoteSwiftPackageReference; + repositoryURL = "https://github.com/google/GoogleSignIn-iOS"; + requirement = { + kind = upToNextMajorVersion; + minimumVersion = 9.0.0; + }; + }; +/* End XCRemoteSwiftPackageReference section */ + +/* Begin XCSwiftPackageProductDependency section */ + 412CF4B02E71B7C300EADB15 /* GoogleSignIn */ = { + isa = XCSwiftPackageProductDependency; + package = 412CF4A62E71AF4C00EADB15 /* XCRemoteSwiftPackageReference "GoogleSignIn-iOS" */; + productName = GoogleSignIn; + }; +/* End XCSwiftPackageProductDependency section */ }; rootObject = 412CF4402E7037D600EADB15 /* Project object */; } diff --git a/ios/Klottr/Klottr.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved b/ios/Klottr/Klottr.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved new file mode 100644 index 0000000..4a50188 --- /dev/null +++ b/ios/Klottr/Klottr.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -0,0 +1,69 @@ +{ + "originHash" : "68aa00e3cba36db2e0a76f54dbc84d1f079d8aa9a19bfa9fce02d6286bd620b7", + "pins" : [ + { + "identity" : "app-check", + "kind" : "remoteSourceControl", + "location" : "https://github.com/google/app-check.git", + "state" : { + "revision" : "61b85103a1aeed8218f17c794687781505fbbef5", + "version" : "11.2.0" + } + }, + { + "identity" : "appauth-ios", + "kind" : "remoteSourceControl", + "location" : "https://github.com/openid/AppAuth-iOS.git", + "state" : { + "revision" : "145104f5ea9d58ae21b60add007c33c1cc0c948e", + "version" : "2.0.0" + } + }, + { + "identity" : "googlesignin-ios", + "kind" : "remoteSourceControl", + "location" : "https://github.com/google/GoogleSignIn-iOS", + "state" : { + "revision" : "3996d908c7b3ce8a87d39c808f9a6b2a08fbe043", + "version" : "9.0.0" + } + }, + { + "identity" : "googleutilities", + "kind" : "remoteSourceControl", + "location" : "https://github.com/google/GoogleUtilities.git", + "state" : { + "revision" : "60da361632d0de02786f709bdc0c4df340f7613e", + "version" : "8.1.0" + } + }, + { + "identity" : "gtm-session-fetcher", + "kind" : "remoteSourceControl", + "location" : "https://github.com/google/gtm-session-fetcher.git", + "state" : { + "revision" : "a2ab612cb980066ee56d90d60d8462992c07f24b", + "version" : "3.5.0" + } + }, + { + "identity" : "gtmappauth", + "kind" : "remoteSourceControl", + "location" : "https://github.com/google/GTMAppAuth.git", + "state" : { + "revision" : "56e0ccf09a6dd29dc7e68bdf729598240ca8aa16", + "version" : "5.0.0" + } + }, + { + "identity" : "promises", + "kind" : "remoteSourceControl", + "location" : "https://github.com/google/promises.git", + "state" : { + "revision" : "540318ecedd63d883069ae7f1ed811a2df00b6ac", + "version" : "2.4.0" + } + } + ], + "version" : 3 +} diff --git a/ios/Klottr/Klottr/AppDelegate.swift b/ios/Klottr/Klottr/AppDelegate.swift new file mode 100644 index 0000000..fc96705 --- /dev/null +++ b/ios/Klottr/Klottr/AppDelegate.swift @@ -0,0 +1,26 @@ +// +// AppDelegate.swift +// Klottr +// +// Created by Olof Hedman on 2025-09-10. +// + + +// iosApp/AppDelegate.swift +import UIKit +import GoogleSignIn +import shared + +class AppDelegate: UIResponder, UIApplicationDelegate { + func application(_ application: UIApplication, + didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { + PlatformAuthStorage.shared.install(delegate: KeychainStorage()) + return true + } + func application(_ app: UIApplication, + open url: URL, + options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool { + if GIDSignIn.sharedInstance.handle(url) { return true } + return false + } +} diff --git a/ios/Klottr/Klottr/ContentView.swift b/ios/Klottr/Klottr/ContentView.swift index c29691d..f68552d 100644 --- a/ios/Klottr/Klottr/ContentView.swift +++ b/ios/Klottr/Klottr/ContentView.swift @@ -5,20 +5,30 @@ // Created by Olof Hedman on 2025-09-09. // +import shared import SwiftUI struct ContentView: View { + var sharedPing = SharedPing() var body: some View { VStack { Image(systemName: "globe") .imageScale(.large) .foregroundStyle(.tint) Text("Hello, world!") + Text(sharedPing.ping()) + Button("Tap me") { + var result = sharedPing.ping() + NSLog("Ping result: \(result)") + + } } .padding() } } + + #Preview { ContentView() } diff --git a/ios/Klottr/Klottr/GoogleAuthError.swift b/ios/Klottr/Klottr/GoogleAuthError.swift new file mode 100644 index 0000000..d48700b --- /dev/null +++ b/ios/Klottr/Klottr/GoogleAuthError.swift @@ -0,0 +1,63 @@ +// +// GoogleAuthError.swift +// Klottr +// +// Created by Olof Hedman on 2025-09-10. +// + + +// iosApp/GoogleAuthIOS.swift +import UIKit +import GoogleSignIn +import shared // Kotlin framework from :shared + +enum GoogleAuthError: Error { case noPresenter } + +enum GoogleAuthIOS { + /// Optional: reads client IDs from Info.plist and sets configuration. + static func configureFromPlist() { + if let clientID = Bundle.main.object(forInfoDictionaryKey: "GIDClientID") as? String { + let serverID = Bundle.main.object(forInfoDictionaryKey: "GIDServerClientID") as? String + GIDSignIn.sharedInstance.configuration = GIDConfiguration( + clientID: clientID, + serverClientID: serverID + ) + } + } + + /// Interactive sign-in. Call from a button handler. + @MainActor + static func signIn(presenting: UIViewController) async throws { + let result = try await GIDSignIn.sharedInstance.signIn(withPresenting: presenting) // iOS + let user = try await result.user.refreshTokensIfNeeded() + let idToken = user.idToken?.tokenString + let sub = user.userID + let email = user.profile?.email + AuthShared.shared.applyLogin(token: idToken, sub: sub, mail: email) + } + + /// Silent restore. Call on app start. + @MainActor + static func restorePreviousSignIn() async { + do { + if GIDSignIn.sharedInstance.hasPreviousSignIn() { + let user = try await GIDSignIn.sharedInstance.restorePreviousSignIn() + let refreshed = try await user.refreshTokensIfNeeded() + AuthShared.shared.applyLogin( + token: refreshed.idToken?.tokenString, + sub: refreshed.userID, + mail: refreshed.profile?.email + ) + } else { + AuthShared.shared.loadFromStorage() // falls back to saved ID token/email + } + } catch { + AuthShared.shared.loadFromStorage() + } + } + + static func signOut() { + GIDSignIn.sharedInstance.signOut() + AuthShared.shared.clearAll() + } +} diff --git a/ios/Klottr/Klottr/Info.plist b/ios/Klottr/Klottr/Info.plist new file mode 100644 index 0000000..566171a --- /dev/null +++ b/ios/Klottr/Klottr/Info.plist @@ -0,0 +1,32 @@ + + + + + + CFBundleIdentifier + $(PRODUCT_BUNDLE_IDENTIFIER) + CFBundleName + $(PRODUCT_NAME) + CFBundleExecutable + $(EXECUTABLE_NAME) + CFBundlePackageType + APPL + + + GIDClientID + YOUR_IOS_CLIENT_ID + GIDServerClientID + YOUR_WEB_CLIENT_ID + + + CFBundleURLTypes + + + CFBundleURLSchemes + + YOUR_REVERSED_IOS_CLIENT_ID + + + + + diff --git a/ios/Klottr/Klottr/KeychainStorage.swift b/ios/Klottr/Klottr/KeychainStorage.swift new file mode 100644 index 0000000..9fe580a --- /dev/null +++ b/ios/Klottr/Klottr/KeychainStorage.swift @@ -0,0 +1,57 @@ +// +// KeychainStorage.swift +// Klottr +// +// Created by Olof Hedman on 2025-09-10. +// + + +// iosApp/KeychainStorage.swift +import Foundation +import Security +import shared // your XCFramework module name + +final class KeychainStorage: NSObject, AuthStorageDelegate { + private let service = "app.klottr.auth" + + func get(key: String) -> String? { + let q: [String: Any] = [ + kSecClass as String: kSecClassGenericPassword, + kSecAttrService as String: service, + kSecAttrAccount as String: key, + kSecReturnData as String: true, + kSecMatchLimit as String: kSecMatchLimitOne + ] + var out: AnyObject? + let status = SecItemCopyMatching(q as CFDictionary, &out) + guard status == errSecSuccess, let data = out as? Data else { return nil } + return String(data: data, encoding: .utf8) + } + + func set(key: String, value: String?) { + let base: [String: Any] = [ + kSecClass as String: kSecClassGenericPassword, + kSecAttrService as String: service, + kSecAttrAccount as String: key + ] + if value == nil { + SecItemDelete(base as CFDictionary) + return + } + let attrs = [kSecValueData as String: value!.data(using: .utf8)!] + let status = SecItemUpdate(base as CFDictionary, attrs as CFDictionary) + if status == errSecItemNotFound { + var add = base + add[kSecValueData as String] = value!.data(using: .utf8)! + SecItemAdd(add as CFDictionary, nil) + } + } + + func clear() { + let q: [String: Any] = [ + kSecClass as String: kSecClassGenericPassword, + kSecAttrService as String: service + ] + SecItemDelete(q as CFDictionary) + } +} diff --git a/ios/Klottr/Klottr/KlottrApp.swift b/ios/Klottr/Klottr/KlottrApp.swift index be665cb..524a478 100644 --- a/ios/Klottr/Klottr/KlottrApp.swift +++ b/ios/Klottr/Klottr/KlottrApp.swift @@ -9,6 +9,9 @@ import SwiftUI @main struct KlottrApp: App { + // Make AppDelegate participate in the SwiftUI lifecycle + @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate + var body: some Scene { WindowGroup { ContentView() diff --git a/shared/src/androidMain/kotlin/app/klottr/util/PlatformAuthStorage.android.kt b/shared/src/androidMain/kotlin/app/klottr/util/PlatformAuthStorage.android.kt new file mode 100644 index 0000000..09a6136 --- /dev/null +++ b/shared/src/androidMain/kotlin/app/klottr/util/PlatformAuthStorage.android.kt @@ -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() + 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 */ } +} diff --git a/shared/src/commonMain/kotlin/app/klottr/util/AuthShared.kt b/shared/src/commonMain/kotlin/app/klottr/util/AuthShared.kt new file mode 100644 index 0000000..6402e6a --- /dev/null +++ b/shared/src/commonMain/kotlin/app/klottr/util/AuthShared.kt @@ -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 = _loggedIn + + private val _email = MutableStateFlow(null) + val emailFlow: StateFlow = _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() + } +} diff --git a/shared/src/commonMain/kotlin/app/klottr/util/AuthStorageDelegate.kt b/shared/src/commonMain/kotlin/app/klottr/util/AuthStorageDelegate.kt new file mode 100644 index 0000000..4172797 --- /dev/null +++ b/shared/src/commonMain/kotlin/app/klottr/util/AuthStorageDelegate.kt @@ -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() +} diff --git a/shared/src/commonMain/kotlin/app/klottr/util/PlatformAuthStorage.kt b/shared/src/commonMain/kotlin/app/klottr/util/PlatformAuthStorage.kt new file mode 100644 index 0000000..931b076 --- /dev/null +++ b/shared/src/commonMain/kotlin/app/klottr/util/PlatformAuthStorage.kt @@ -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) + +} diff --git a/shared/src/iosMain/kotlin/app/klottr/util/PlatformAuthStorage.ios.kt b/shared/src/iosMain/kotlin/app/klottr/util/PlatformAuthStorage.ios.kt new file mode 100644 index 0000000..e7058f7 --- /dev/null +++ b/shared/src/iosMain/kotlin/app/klottr/util/PlatformAuthStorage.ios.kt @@ -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() } +}