Skip to content

Open source CapCut alternative.

TypeScript

fix(ios): the app opens on the session it already has

David Le7401b717e717
diff --git a/apps/ios/Donkey/Controllers/AuthController.swift b/apps/ios/Donkey/Controllers/AuthController.swift
index 98d2a743..15d5bbf2 100644
--- a/apps/ios/Donkey/Controllers/AuthController.swift
+++ b/apps/ios/Donkey/Controllers/AuthController.swift
@@ -65,26 +65,33 @@ final class AuthController: NSObject, AuthServicing {
return profile
}
- func restoreSession() async -> UserProfile? {
+ /// Keychain only — this runs while the first frame is being built.
+ func storedSession() -> StoredSession {
+ guard KeychainStore.read(Self.tokenKey) != nil else { return .none }
+ guard let profile = KeychainStore.read(Self.profileKey)
+ .flatMap({ try? JSONDecoder().decode(UserProfile.self, from: $0) })
+ else { return .unknown }
+ return .cached(profile)
+ }
+
+ func checkSession() async -> SessionCheck {
guard let tokenData = KeychainStore.read(Self.tokenKey),
- let token = String(data: tokenData, encoding: .utf8) else { return nil }
- let cached = KeychainStore.read(Self.profileKey)
- .flatMap { try? JSONDecoder().decode(UserProfile.self, from: $0) }
+ let token = String(data: tokenData, encoding: .utf8) else { return .rejected }
do {
let profile = try await fetchProfile(token: token)
if let data = try? JSONEncoder().encode(profile) {
KeychainStore.save(data, for: Self.profileKey)
}
- return profile
+ return .valid(profile)
} catch AuthError.server {
// The backend rejected the session: signed out for real.
KeychainStore.delete(Self.tokenKey)
KeychainStore.delete(Self.profileKey)
- return nil
+ return .rejected
} catch {
- // Offline: recording and viewing are local, so the cached
- // profile keeps the app usable.
- return cached
+ // Offline: recording and viewing are local, so the session the
+ // app opened on keeps standing.
+ return .unreachable
}
}
diff --git a/apps/ios/DonkeyKit/Sources/DonkeyKitModels/AuthModel.swift b/apps/ios/DonkeyKit/Sources/DonkeyKitModels/AuthModel.swift
index dbb8c0e4..68da0476 100644
--- a/apps/ios/DonkeyKit/Sources/DonkeyKitModels/AuthModel.swift
+++ b/apps/ios/DonkeyKit/Sources/DonkeyKitModels/AuthModel.swift
@@ -39,14 +39,37 @@ nonisolated public enum AuthProvider: String, Sendable {
case google, apple
}
+/// What this device holds before anything is asked of the network.
+nonisolated public enum StoredSession: Equatable, Sendable {
+ /// Nothing stored: this is a sign-in.
+ case none
+ /// A session, with the profile it was last seen wearing.
+ case cached(UserProfile)
+ /// A session whose profile this device has never read — the first launch
+ /// after a sign-in that could not write its cache.
+ case unknown
+}
+
+/// What the server said about the stored session.
+nonisolated public enum SessionCheck: Equatable, Sendable {
+ case valid(UserProfile)
+ /// The backend rejected it: signed out for real.
+ case rejected
+ /// The server could not be reached, so whatever is cached still stands.
+ case unreachable
+}
+
/// Native sign-in and session exchange, implemented by the app target's
/// AuthController against the Donkey Cut backend.
public protocol AuthServicing: AnyObject {
/// Runs the provider's native sign-in flow, exchanges the identity token
/// for a backend session, and returns the signed-in user.
func signIn(with provider: AuthProvider) async throws -> UserProfile
- /// The user restored from the stored session, if one exists.
- func restoreSession() async -> UserProfile?
+ /// The session on this device, read from local storage with nothing on
+ /// the network in the way. Called while the first frame is being built.
+ func storedSession() -> StoredSession
+ /// Put the stored session to the server and take its answer.
+ func checkSession() async -> SessionCheck
func signOut() async
}
@@ -59,13 +82,22 @@ nonisolated public enum AuthState: Equatable, Sendable {
@Observable
public final class AuthModel {
- public private(set) var state: AuthState = .restoring
+ public private(set) var state: AuthState
public private(set) var lastError: String?
private let service: any AuthServicing
+ /// The app opens on the session this device already holds, so a returning
+ /// user is looking at their own screens in the first frame and someone
+ /// signing in is looking at the sign-in buttons. Only a session whose
+ /// profile was never cached has to wait, and only that one shows the gate.
public init(service: any AuthServicing) {
self.service = service
+ state = switch service.storedSession() {
+ case .cached(let profile): .signedIn(profile)
+ case .unknown: .restoring
+ case .none: .signedOut
+ }
}
public var user: UserProfile? {
@@ -75,11 +107,15 @@ public final class AuthModel {
public var isSignedIn: Bool { user != nil }
+ /// Check the session the app opened on. A rejection signs out; a server
+ /// that cannot be reached leaves a cached session standing, because
+ /// recording and viewing are local.
public func restore() async {
- if let profile = await service.restoreSession() {
- state = .signedIn(profile)
- } else {
- state = .signedOut
+ guard state != .signedOut else { return }
+ switch await service.checkSession() {
+ case .valid(let profile): state = .signedIn(profile)
+ case .rejected: state = .signedOut
+ case .unreachable: if !isSignedIn { state = .signedOut }
}
}
diff --git a/apps/ios/DonkeyKit/Tests/DonkeyKitModelsTests/DonkeyKitModelsTests.swift b/apps/ios/DonkeyKit/Tests/DonkeyKitModelsTests/DonkeyKitModelsTests.swift
index c3cfba2f..cd9a735e 100644
--- a/apps/ios/DonkeyKit/Tests/DonkeyKitModelsTests/DonkeyKitModelsTests.swift
+++ b/apps/ios/DonkeyKit/Tests/DonkeyKitModelsTests/DonkeyKitModelsTests.swift
@@ -659,15 +659,21 @@ import Testing
@MainActor
@Suite struct AuthModelTests {
final class FakeAuth: AuthServicing {
- var stored: UserProfile?
+ var stored: StoredSession = .none
+ var check: SessionCheck = .rejected
var result: Result<UserProfile, any Error> = .failure(CancellationError())
+ var checked = false
func signIn(with provider: AuthProvider) async throws -> UserProfile {
try result.get()
}
- func restoreSession() async -> UserProfile? { stored }
- func signOut() async { stored = nil }
+ func storedSession() -> StoredSession { stored }
+ func checkSession() async -> SessionCheck {
+ checked = true
+ return check
+ }
+ func signOut() async { stored = .none }
}
@Test func restoreWithoutSessionSignsOut() async {
@@ -676,6 +682,45 @@ import Testing
#expect(model.state == .signedOut)
}
+ @Test func cachedSessionIsSignedInBeforeAnythingIsChecked() {
+ let service = FakeAuth()
+ let profile = UserProfile(id: "1", name: "David", email: "[email protected]")
+ service.stored = .cached(profile)
+ let model = AuthModel(service: service)
+ #expect(model.state == .signedIn(profile))
+ #expect(service.checked == false)
+ }
+
+ @Test func sessionWithNoCachedProfileWaitsOnTheCheck() async {
+ let service = FakeAuth()
+ service.stored = .unknown
+ let profile = UserProfile(id: "1", name: "David", email: "[email protected]")
+ service.check = .valid(profile)
+ let model = AuthModel(service: service)
+ #expect(model.state == .restoring)
+ await model.restore()
+ #expect(model.state == .signedIn(profile))
+ }
+
+ @Test func unreachableServerKeepsTheCachedSession() async {
+ let service = FakeAuth()
+ let profile = UserProfile(id: "1", name: "David", email: "[email protected]")
+ service.stored = .cached(profile)
+ service.check = .unreachable
+ let model = AuthModel(service: service)
+ await model.restore()
+ #expect(model.state == .signedIn(profile))
+ }
+
+ @Test func rejectedSessionSignsOut() async {
+ let service = FakeAuth()
+ service.stored = .cached(UserProfile(id: "1", name: "David", email: "[email protected]"))
+ service.check = .rejected
+ let model = AuthModel(service: service)
+ await model.restore()
+ #expect(model.state == .signedOut)
+ }
+
@Test func successfulSignIn() async {
let service = FakeAuth()
let profile = UserProfile(id: "1", name: "David", email: "[email protected]")
diff --git a/docs/guides/ios-app.md b/docs/guides/ios-app.md
index e11e4a94..83a474f2 100644
--- a/docs/guides/ios-app.md
+++ b/docs/guides/ios-app.md
@@ -72,6 +72,12 @@ rather than to the screen, so leaving the player and coming back finds it
where it was, and a project that already carries a render offers it as a
choice that saves right away without spending another.
+The app opens on the session already on the device: the keychain is read while
+the first frame is built, and the server check that follows only corrects it —
+a rejection signs out, a server that cannot be reached leaves the session
+standing, since recording and viewing are local. Nothing waits on the network
+to draw.
+
Every request carries an `x-donkey-cut-client: ios` header. The server
remembers accounts it has seen it from, and the desktop shows its phone
surfaces (Camera Roll and Notes tabs) only to those accounts.