More UI Changes? Not sure why they didn't go previously

This commit is contained in:
2026-03-20 02:06:19 -05:00
parent efec14139c
commit 2110c13ea1
24 changed files with 1688 additions and 70 deletions

58
LabWise/AppState.swift Normal file
View File

@@ -0,0 +1,58 @@
import SwiftUI
import LabWiseKit
@Observable
final class AppState {
var isAuthenticated: Bool = false
var currentUser: AuthUser?
static let shared = AppState()
private let authClient = AuthClient()
private init() {
// Wire 401 handler
APIClient.shared.onUnauthorized = { [weak self] in
guard let self else { return }
Task { @MainActor in
self.signedOut()
}
}
// If a session cookie exists, validate it with the server on launch
let hasCookie = HTTPCookieStorage.shared.cookies?.contains {
$0.name == "__Secure-better-auth.session_token" || $0.name == "better-auth.session_token"
} ?? false
if hasCookie {
// Optimistically show the authenticated UI, then validate
isAuthenticated = true
Task { @MainActor [weak self] in
await self?.validateExistingSession()
}
}
}
@MainActor
private func validateExistingSession() async {
do {
let user = try await authClient.fetchCurrentUser()
currentUser = user
isAuthenticated = true
} catch {
// Cookie was stale or invalid
signedOut()
}
}
@MainActor
func signedIn(user: AuthUser) {
currentUser = user
isAuthenticated = true
}
@MainActor
func signedOut() {
currentUser = nil
isAuthenticated = false
}
}