Move login/registration server state to datastore

This commit is contained in:
William Brawner 2022-05-17 20:33:38 -06:00
parent c361728ae1
commit 47754aac27
3 changed files with 12 additions and 23 deletions

View file

@ -32,7 +32,8 @@ class DataStore : ObservableObject {
_ apiService: TwigsApiService _ apiService: TwigsApiService
) { ) {
self.apiService = apiService self.apiService = apiService
self.baseUrl = UserDefaults.standard.string(forKey: KEY_BASE_URL) self.baseUrl = UserDefaults.standard.string(forKey: KEY_BASE_URL) ?? ""
self.apiService.baseUrl = baseUrl
self.token = UserDefaults.standard.string(forKey: KEY_TOKEN) self.token = UserDefaults.standard.string(forKey: KEY_TOKEN)
self.userId = UserDefaults.standard.string(forKey: KEY_USER_ID) self.userId = UserDefaults.standard.string(forKey: KEY_USER_ID)
} }
@ -350,7 +351,7 @@ class DataStore : ObservableObject {
private let KEY_TOKEN = "TOKEN" private let KEY_TOKEN = "TOKEN"
private let KEY_USER_ID = "USER_ID" private let KEY_USER_ID = "USER_ID"
@Published var baseUrl: String? { @Published var baseUrl: String {
didSet { didSet {
self.apiService.baseUrl = baseUrl self.apiService.baseUrl = baseUrl
UserDefaults.standard.set(baseUrl, forKey: KEY_BASE_URL) UserDefaults.standard.set(baseUrl, forKey: KEY_BASE_URL)
@ -369,12 +370,8 @@ class DataStore : ObservableObject {
} }
@Published var showLogin: Bool = true @Published var showLogin: Bool = true
func login(server: String, username: String, password: String) async { func login(username: String, password: String) async {
self.currentUser = .loading self.currentUser = .loading
self.apiService.baseUrl = server
// The API Service applies some validation and correcting of the server before returning it so we use that
// value instead of the original one
self.baseUrl = self.apiService.baseUrl ?? ""
do { do {
let response = try await self.apiService.login(username: username, password: password) let response = try await self.apiService.login(username: username, password: password)
self.token = response.token self.token = response.token
@ -391,17 +388,12 @@ class DataStore : ObservableObject {
} }
} }
func register(server: String, username: String, email: String, password: String, confirmPassword: String) async { func register(username: String, email: String, password: String, confirmPassword: String) async {
// TODO: Validate other fields as well // TODO: Validate other fields as well
if !password.elementsEqual(confirmPassword) { if !password.elementsEqual(confirmPassword) {
// TODO: Show error message to user // TODO: Show error message to user
return return
} }
self.apiService.baseUrl = server
// The API Service applies some validation and correcting of the server before returning it so we use that
// value instead of the original one
self.baseUrl = self.apiService.baseUrl ?? ""
do { do {
_ = try await apiService.register(username: username, email: email, password: password) _ = try await apiService.register(username: username, email: email, password: password)
} catch { } catch {
@ -413,7 +405,7 @@ class DataStore : ObservableObject {
} }
return return
} }
await self.login(server: server, username: username, password: password) await self.login(username: username, password: password)
} }
func loadProfile() async { func loadProfile() async {

View file

@ -10,7 +10,6 @@ import SwiftUI
import Combine import Combine
struct LoginView: View { struct LoginView: View {
@State var server: String = ""
@State var username: String = "" @State var username: String = ""
@State var password: String = "" @State var password: String = ""
@EnvironmentObject var dataStore: DataStore @EnvironmentObject var dataStore: DataStore
@ -31,7 +30,7 @@ struct LoginView: View {
NavigationView { NavigationView {
VStack { VStack {
Text("info_login") Text("info_login")
TextField(LocalizedStringKey("prompt_server"), text: self.$server) TextField(LocalizedStringKey("prompt_server"), text: self.$dataStore.baseUrl)
.autocapitalization(.none) .autocapitalization(.none)
.textFieldStyle(RoundedBorderTextFieldStyle()) .textFieldStyle(RoundedBorderTextFieldStyle())
.textContentType(.URL) .textContentType(.URL)
@ -47,12 +46,12 @@ struct LoginView: View {
.textContentType(.password) .textContentType(.password)
Button("action_login", action: { Button("action_login", action: {
Task { Task {
await self.dataStore.login(server: self.server, username: self.username, password: self.password) await self.dataStore.login(username: self.username, password: self.password)
} }
}).buttonStyle(DefaultButtonStyle()) }).buttonStyle(DefaultButtonStyle())
Spacer() Spacer()
Text("info_register") Text("info_register")
NavigationLink(destination: RegistrationView(server: self.$server)) { NavigationLink(destination: RegistrationView(username: self.$username, password: self.$password)) {
Text("action_register") Text("action_register")
.buttonStyle(DefaultButtonStyle()) .buttonStyle(DefaultButtonStyle())
} }

View file

@ -9,16 +9,15 @@
import SwiftUI import SwiftUI
struct RegistrationView: View { struct RegistrationView: View {
@Binding var server: String @Binding var username: String
@State var username: String = ""
@State var email: String = "" @State var email: String = ""
@State var password: String = "" @Binding var password: String
@State var confirmedPassword: String = "" @State var confirmedPassword: String = ""
@EnvironmentObject var dataStore: DataStore @EnvironmentObject var dataStore: DataStore
var body: some View { var body: some View {
VStack { VStack {
TextField(LocalizedStringKey("prompt_server"), text: self.$server) TextField(LocalizedStringKey("prompt_server"), text: self.$dataStore.baseUrl)
.textFieldStyle(RoundedBorderTextFieldStyle()) .textFieldStyle(RoundedBorderTextFieldStyle())
.textContentType(.URL) .textContentType(.URL)
TextField("prompt_username", text: self.$username) TextField("prompt_username", text: self.$username)
@ -38,7 +37,6 @@ struct RegistrationView: View {
Button("action_register", action: { Button("action_register", action: {
Task { Task {
await self.dataStore.register( await self.dataStore.register(
server: self.server,
username: self.username, username: self.username,
email: self.email, email: self.email,
password: self.password, password: self.password,