auth and projects

This commit is contained in:
vadim committed 2026-10-29 13:14:46 +03:00
1 parent 558bb005f4
commit 0ba2db0ec2
14 files changed
+1207 -94

No files matched your search

+59
View File
@@ -0,0 +1,59 @@
//
// AuthStore.swift
// Snail
//
import Foundation
import Observation
@Observable
final class AuthStore {
enum State: Equatable {
case loading
case signedOut
case signedIn(User)
}
private(set) var state: State
private let service: AuthService
init(service: AuthService = AuthService(), state: State = .loading) {
self.service = service
self.state = state
}
var currentUser: User? {
if case .signedIn(let user) = state { user } else { nil }
}
/// Проверяет сохранённую cookie сессии при запуске.
func restoreSession() async {
do {
state = try await service.currentUser().map(State.signedIn) ?? .signedOut
} catch {
state = .signedOut
}
}
func sendCode(to email: String) async throws {
try await service.sendSignInCode(to: email)
}
func signIn(email: String, code: String) async throws {
state = .signedIn(try await service.signIn(email: email, code: code))
}
/// Обновляет профиль; если сессия больше не действительна — разлогинивает.
func refreshUser() async {
do {
state = try await service.currentUser().map(State.signedIn) ?? .signedOut
} catch {
// Сетевая ошибка — оставляем текущие данные.
}
}
func signOut() async {
await service.signOut()
state = .signedOut
}
}