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:
@@ -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.GoogleSignInAccount
|
||||||
import com.google.android.gms.auth.api.signin.GoogleSignInClient
|
import com.google.android.gms.auth.api.signin.GoogleSignInClient
|
||||||
import com.google.android.gms.auth.api.signin.GoogleSignInOptions
|
import com.google.android.gms.auth.api.signin.GoogleSignInOptions
|
||||||
|
import app.klottr.util.AuthShared
|
||||||
|
|
||||||
object Auth {
|
object Auth {
|
||||||
|
|
||||||
@@ -50,6 +51,8 @@ object Auth {
|
|||||||
email = sp.getString(KEY_EMAIL, null)
|
email = sp.getString(KEY_EMAIL, null)
|
||||||
_loggedIn.value = idToken != null
|
_loggedIn.value = idToken != null
|
||||||
_email.value = email
|
_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. */
|
/** Spara kontoinfo efter lyckad inloggning och uppdatera flöden. */
|
||||||
@@ -68,6 +71,8 @@ object Auth {
|
|||||||
}
|
}
|
||||||
_loggedIn.value = token != null
|
_loggedIn.value = token != null
|
||||||
_email.value = mail
|
_email.value = mail
|
||||||
|
// Sync shared KMP state
|
||||||
|
AuthShared.applyLogin(token, sub, mail)
|
||||||
if (token == null) {
|
if (token == null) {
|
||||||
Log.w("Klottr/Auth", "ID‑token är null efter sign‑in. Kontrollera WEB_CLIENT_ID i Auth.kt och på servern.")
|
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()
|
signInClient(ctx).signOut()
|
||||||
_loggedIn.value = false
|
_loggedIn.value = false
|
||||||
_email.value = null
|
_email.value = null
|
||||||
|
// Clear shared KMP state too
|
||||||
|
AuthShared.clearAll()
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -6,6 +6,11 @@
|
|||||||
objectVersion = 77;
|
objectVersion = 77;
|
||||||
objects = {
|
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 */
|
/* Begin PBXContainerItemProxy section */
|
||||||
412CF4592E7037D800EADB15 /* PBXContainerItemProxy */ = {
|
412CF4592E7037D800EADB15 /* PBXContainerItemProxy */ = {
|
||||||
isa = PBXContainerItemProxy;
|
isa = PBXContainerItemProxy;
|
||||||
@@ -27,11 +32,25 @@
|
|||||||
412CF4482E7037D700EADB15 /* Klottr.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Klottr.app; sourceTree = BUILT_PRODUCTS_DIR; };
|
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; };
|
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; };
|
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 = "<absolute>"; };
|
||||||
/* End PBXFileReference section */
|
/* 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 */
|
/* Begin PBXFileSystemSynchronizedRootGroup section */
|
||||||
412CF44A2E7037D700EADB15 /* Klottr */ = {
|
412CF44A2E7037D700EADB15 /* Klottr */ = {
|
||||||
isa = PBXFileSystemSynchronizedRootGroup;
|
isa = PBXFileSystemSynchronizedRootGroup;
|
||||||
|
exceptions = (
|
||||||
|
412CF4B42E71C14D00EADB15 /* Exceptions for "Klottr" folder in "Klottr" target */,
|
||||||
|
);
|
||||||
path = Klottr;
|
path = Klottr;
|
||||||
sourceTree = "<group>";
|
sourceTree = "<group>";
|
||||||
};
|
};
|
||||||
@@ -52,6 +71,8 @@
|
|||||||
isa = PBXFrameworksBuildPhase;
|
isa = PBXFrameworksBuildPhase;
|
||||||
buildActionMask = 2147483647;
|
buildActionMask = 2147483647;
|
||||||
files = (
|
files = (
|
||||||
|
412CF4A32E719E7D00EADB15 /* shared.xcframework in Frameworks */,
|
||||||
|
412CF4B12E71B7C300EADB15 /* GoogleSignIn in Frameworks */,
|
||||||
);
|
);
|
||||||
runOnlyForDeploymentPostprocessing = 0;
|
runOnlyForDeploymentPostprocessing = 0;
|
||||||
};
|
};
|
||||||
@@ -75,9 +96,11 @@
|
|||||||
412CF43F2E7037D600EADB15 = {
|
412CF43F2E7037D600EADB15 = {
|
||||||
isa = PBXGroup;
|
isa = PBXGroup;
|
||||||
children = (
|
children = (
|
||||||
|
412CF4A22E719E7D00EADB15 /* shared.xcframework */,
|
||||||
412CF44A2E7037D700EADB15 /* Klottr */,
|
412CF44A2E7037D700EADB15 /* Klottr */,
|
||||||
412CF45B2E7037D800EADB15 /* KlottrTests */,
|
412CF45B2E7037D800EADB15 /* KlottrTests */,
|
||||||
412CF4652E7037D800EADB15 /* KlottrUITests */,
|
412CF4652E7037D800EADB15 /* KlottrUITests */,
|
||||||
|
412CF4AF2E71B7C300EADB15 /* Frameworks */,
|
||||||
412CF4492E7037D700EADB15 /* Products */,
|
412CF4492E7037D700EADB15 /* Products */,
|
||||||
);
|
);
|
||||||
sourceTree = "<group>";
|
sourceTree = "<group>";
|
||||||
@@ -92,6 +115,13 @@
|
|||||||
name = Products;
|
name = Products;
|
||||||
sourceTree = "<group>";
|
sourceTree = "<group>";
|
||||||
};
|
};
|
||||||
|
412CF4AF2E71B7C300EADB15 /* Frameworks */ = {
|
||||||
|
isa = PBXGroup;
|
||||||
|
children = (
|
||||||
|
);
|
||||||
|
name = Frameworks;
|
||||||
|
sourceTree = "<group>";
|
||||||
|
};
|
||||||
/* End PBXGroup section */
|
/* End PBXGroup section */
|
||||||
|
|
||||||
/* Begin PBXNativeTarget section */
|
/* Begin PBXNativeTarget section */
|
||||||
@@ -112,6 +142,7 @@
|
|||||||
);
|
);
|
||||||
name = Klottr;
|
name = Klottr;
|
||||||
packageProductDependencies = (
|
packageProductDependencies = (
|
||||||
|
412CF4B02E71B7C300EADB15 /* GoogleSignIn */,
|
||||||
);
|
);
|
||||||
productName = Klottr;
|
productName = Klottr;
|
||||||
productReference = 412CF4482E7037D700EADB15 /* Klottr.app */;
|
productReference = 412CF4482E7037D700EADB15 /* Klottr.app */;
|
||||||
@@ -195,6 +226,9 @@
|
|||||||
);
|
);
|
||||||
mainGroup = 412CF43F2E7037D600EADB15;
|
mainGroup = 412CF43F2E7037D600EADB15;
|
||||||
minimizedProjectReferenceProxies = 1;
|
minimizedProjectReferenceProxies = 1;
|
||||||
|
packageReferences = (
|
||||||
|
412CF4A62E71AF4C00EADB15 /* XCRemoteSwiftPackageReference "GoogleSignIn-iOS" */,
|
||||||
|
);
|
||||||
preferredProjectObjectVersion = 77;
|
preferredProjectObjectVersion = 77;
|
||||||
productRefGroup = 412CF4492E7037D700EADB15 /* Products */;
|
productRefGroup = 412CF4492E7037D700EADB15 /* Products */;
|
||||||
projectDirPath = "";
|
projectDirPath = "";
|
||||||
@@ -398,7 +432,9 @@
|
|||||||
DEVELOPMENT_ASSET_PATHS = "\"Klottr/Preview Content\"";
|
DEVELOPMENT_ASSET_PATHS = "\"Klottr/Preview Content\"";
|
||||||
DEVELOPMENT_TEAM = 9LY47TPGT7;
|
DEVELOPMENT_TEAM = 9LY47TPGT7;
|
||||||
ENABLE_PREVIEWS = YES;
|
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_UIApplicationSceneManifest_Generation = YES;
|
||||||
INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES;
|
INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES;
|
||||||
INFOPLIST_KEY_UILaunchScreen_Generation = YES;
|
INFOPLIST_KEY_UILaunchScreen_Generation = YES;
|
||||||
@@ -427,7 +463,9 @@
|
|||||||
DEVELOPMENT_ASSET_PATHS = "\"Klottr/Preview Content\"";
|
DEVELOPMENT_ASSET_PATHS = "\"Klottr/Preview Content\"";
|
||||||
DEVELOPMENT_TEAM = 9LY47TPGT7;
|
DEVELOPMENT_TEAM = 9LY47TPGT7;
|
||||||
ENABLE_PREVIEWS = YES;
|
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_UIApplicationSceneManifest_Generation = YES;
|
||||||
INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES;
|
INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES;
|
||||||
INFOPLIST_KEY_UILaunchScreen_Generation = YES;
|
INFOPLIST_KEY_UILaunchScreen_Generation = YES;
|
||||||
@@ -558,6 +596,25 @@
|
|||||||
defaultConfigurationName = Release;
|
defaultConfigurationName = Release;
|
||||||
};
|
};
|
||||||
/* End XCConfigurationList section */
|
/* 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 */;
|
rootObject = 412CF4402E7037D600EADB15 /* Project object */;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
|
||||||
|
}
|
||||||
26
ios/Klottr/Klottr/AppDelegate.swift
Normal file
26
ios/Klottr/Klottr/AppDelegate.swift
Normal file
@@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,20 +5,30 @@
|
|||||||
// Created by Olof Hedman on 2025-09-09.
|
// Created by Olof Hedman on 2025-09-09.
|
||||||
//
|
//
|
||||||
|
|
||||||
|
import shared
|
||||||
import SwiftUI
|
import SwiftUI
|
||||||
|
|
||||||
struct ContentView: View {
|
struct ContentView: View {
|
||||||
|
var sharedPing = SharedPing()
|
||||||
var body: some View {
|
var body: some View {
|
||||||
VStack {
|
VStack {
|
||||||
Image(systemName: "globe")
|
Image(systemName: "globe")
|
||||||
.imageScale(.large)
|
.imageScale(.large)
|
||||||
.foregroundStyle(.tint)
|
.foregroundStyle(.tint)
|
||||||
Text("Hello, world!")
|
Text("Hello, world!")
|
||||||
|
Text(sharedPing.ping())
|
||||||
|
Button("Tap me") {
|
||||||
|
var result = sharedPing.ping()
|
||||||
|
NSLog("Ping result: \(result)")
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
.padding()
|
.padding()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#Preview {
|
#Preview {
|
||||||
ContentView()
|
ContentView()
|
||||||
}
|
}
|
||||||
|
|||||||
63
ios/Klottr/Klottr/GoogleAuthError.swift
Normal file
63
ios/Klottr/Klottr/GoogleAuthError.swift
Normal file
@@ -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()
|
||||||
|
}
|
||||||
|
}
|
||||||
32
ios/Klottr/Klottr/Info.plist
Normal file
32
ios/Klottr/Klottr/Info.plist
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||||
|
<plist version="1.0">
|
||||||
|
<dict>
|
||||||
|
<!-- Standard stuff -->
|
||||||
|
<key>CFBundleIdentifier</key>
|
||||||
|
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
|
||||||
|
<key>CFBundleName</key>
|
||||||
|
<string>$(PRODUCT_NAME)</string>
|
||||||
|
<key>CFBundleExecutable</key>
|
||||||
|
<string>$(EXECUTABLE_NAME)</string>
|
||||||
|
<key>CFBundlePackageType</key>
|
||||||
|
<string>APPL</string>
|
||||||
|
|
||||||
|
<!-- Google Sign-In config -->
|
||||||
|
<key>GIDClientID</key>
|
||||||
|
<string>YOUR_IOS_CLIENT_ID</string>
|
||||||
|
<key>GIDServerClientID</key>
|
||||||
|
<string>YOUR_WEB_CLIENT_ID</string>
|
||||||
|
|
||||||
|
<!-- URL scheme is required for the OAuth redirect -->
|
||||||
|
<key>CFBundleURLTypes</key>
|
||||||
|
<array>
|
||||||
|
<dict>
|
||||||
|
<key>CFBundleURLSchemes</key>
|
||||||
|
<array>
|
||||||
|
<string>YOUR_REVERSED_IOS_CLIENT_ID</string>
|
||||||
|
</array>
|
||||||
|
</dict>
|
||||||
|
</array>
|
||||||
|
</dict>
|
||||||
|
</plist>
|
||||||
57
ios/Klottr/Klottr/KeychainStorage.swift
Normal file
57
ios/Klottr/Klottr/KeychainStorage.swift
Normal file
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -9,6 +9,9 @@ import SwiftUI
|
|||||||
|
|
||||||
@main
|
@main
|
||||||
struct KlottrApp: App {
|
struct KlottrApp: App {
|
||||||
|
// Make AppDelegate participate in the SwiftUI lifecycle
|
||||||
|
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
|
||||||
|
|
||||||
var body: some Scene {
|
var body: some Scene {
|
||||||
WindowGroup {
|
WindowGroup {
|
||||||
ContentView()
|
ContentView()
|
||||||
|
|||||||
@@ -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