jel/Jel/Controllers/AuthStateController.swift

41 lines
1.0 KiB
Swift

//
// AuthStateController.swift
// Jel
//
// Created by zerocool on 12/11/23.
//
import Foundation
class AuthStateController: ObservableObject {
@Published var loggedIn: Bool
@Published var serverUrl: URL?
@Published var authToken: String?
private let defaults = UserDefaults.standard
static let shared = AuthStateController()
init(loggedIn: Bool = false, serverUrl: URL? = nil, authToken: String? = nil) {
self.loggedIn = loggedIn
self.serverUrl = serverUrl
self.authToken = authToken
}
func load() {
self.loggedIn = defaults.bool(forKey: "AuthState_loggedIn")
if let oldServerUrl = defaults.url(forKey: "AuthState_serverUrl") {
self.serverUrl = oldServerUrl
}
if let oldAuthToken = defaults.string(forKey: "AuthState_authToken") {
self.authToken = oldAuthToken
}
}
func save() {
defaults.set(self.loggedIn, forKey: "AuthState_loggedIn")
defaults.set(self.serverUrl, forKey: "AuthState_serverUrl")
defaults.set(self.authToken, forKey: "AuthState_authToken")
}
}