Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: SPChat/Features/Account/SessionStore.swift3// Description: Observable session state: launch check, passkey sign-in / enrolment, sign-out with cache wipe, reaction to a session ended by the server.4import SwiftUI5import SPChatCore67@Observable final class SessionStore {8 enum Phase: Equatable { case checking, signedOut, signedIn }9 private(set) var phase: Phase = .checking10 private(set) var isWorking = false11 var message: String?12 /// True when the session could not be verified because the server is unreachable (cached history stays readable).13 private(set) var unverified = false14 @ObservationIgnored var onSignedOut: (() -> Void)?15 @ObservationIgnored private let api: APIClient16 @ObservationIgnored private let auth: AuthService17 @ObservationIgnored private let cache: CacheStore?1819 init(api: APIClient, auth: AuthService, cache: CacheStore?) {20 self.api = api; self.auth = auth; self.cache = cache21 api.onSessionEnded = { [weak self] in Task { @MainActor in self?.sessionEndedByServer() } }22 }2324 /// Launch: a stored token opens the app immediately (offline included); the server then confirms or ends it.25 func bootstrap() async {26 guard api.hasSession else { phase = .signedOut; return }27 phase = .signedIn28 await verify()29 }3031 func verify() async {32 guard phase == .signedIn else { return }33 do {34 let state = try await auth.state()35 unverified = false36 if !state.signedIn { api.cookies.clear(); await finishSignOut(message: "Session expirée. Reconnecte-toi avec ta clé d'accès.") }37 } catch { unverified = true }38 }3940 func signIn() async {41 await run { try await self.auth.signIn() }42 }4344 /// First passkey on this Mac from a bootstrap / recovery code typed by the owner.45 func enrol(code: String, label: String) async {46 await run {47 try await self.auth.redeemSetupCode(code)48 try await self.auth.registerPasskey(label: label)49 }50 }5152 private func run(_ work: @escaping () async throws -> Void) async {53 guard !isWorking else { return }54 isWorking = true; message = nil55 defer { isWorking = false }56 do {57 try await work()58 guard api.hasSession else { message = "Le serveur n'a pas ouvert de session."; return }59 unverified = false; phase = .signedIn60 } catch let e as PasskeyError {61 message = e.code == .cancelled ? nil : e.message62 } catch { message = (error as? LocalizedError)?.errorDescription ?? "La vérification n'a pas abouti." }63 }6465 func signOut() async {66 await auth.signOut()67 await finishSignOut(message: nil)68 }6970 private func sessionEndedByServer() {71 guard phase == .signedIn else { return }72 Task { await finishSignOut(message: "Session terminée (révoquée ou expirée). Reconnecte-toi avec ta clé d'accès.") }73 }7475 /// Nothing of the owner's content stays on disk once the session is gone.76 private func finishSignOut(message: String?) async {77 try? await cache?.wipe()78 onSignedOut?()79 self.message = message80 phase = .signedOut81 }82}83