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
) {
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.userId = UserDefaults.standard.string(forKey: KEY_USER_ID)
}
@ -350,7 +351,7 @@ class DataStore : ObservableObject {
private let KEY_TOKEN = "TOKEN"
private let KEY_USER_ID = "USER_ID"
@Published var baseUrl: String? {
@Published var baseUrl: String {
didSet {
self.apiService.baseUrl = baseUrl
UserDefaults.standard.set(baseUrl, forKey: KEY_BASE_URL)
@ -369,12 +370,8 @@ class DataStore : ObservableObject {
}
@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.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 {
let response = try await self.apiService.login(username: username, password: password)
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
if !password.elementsEqual(confirmPassword) {
// TODO: Show error message to user
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 {
_ = try await apiService.register(username: username, email: email, password: password)
} catch {
@ -413,7 +405,7 @@ class DataStore : ObservableObject {
}
return
}
await self.login(server: server, username: username, password: password)
await self.login(username: username, password: password)
}
func loadProfile() async {

View file

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

View file

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