init
This commit is contained in:
1 parent
558bb005f4
commit
9ed2f17768
14 files changed
+1207
-94
No files matched your search
@@ -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
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user