34 lines
790 B
Swift
34 lines
790 B
Swift
import Combine
|
|
|
|
@MainActor
|
|
final class AuthSessionStore: ObservableObject {
|
|
enum State {
|
|
case checking
|
|
case signedOut
|
|
case signedIn
|
|
}
|
|
|
|
@Published private(set) var state: State = .checking
|
|
@Published private(set) var isSigningIn = false
|
|
|
|
private let api = MusicAPI()
|
|
|
|
func restoreSession() async {
|
|
guard state == .checking else { return }
|
|
|
|
do {
|
|
state = try await api.sessionStatus().authenticated ? .signedIn : .signedOut
|
|
} catch {
|
|
state = .signedOut
|
|
}
|
|
}
|
|
|
|
func signIn(email: String, password: String) async throws {
|
|
isSigningIn = true
|
|
defer { isSigningIn = false }
|
|
|
|
try await api.signIn(email: email, password: password)
|
|
state = .signedIn
|
|
}
|
|
}
|