|
@@ -7,39 +7,93 @@
|
7
|
7
|
|
8
|
8
|
import SwiftUI
|
9
|
9
|
|
|
10
|
+class LoginViewModel: ObservableObject {
|
|
11
|
+ @Published var response: Bool?
|
|
12
|
+
|
|
13
|
+ func changeResponse(response: Bool) {
|
|
14
|
+ self.response = response
|
|
15
|
+ }
|
|
16
|
+
|
|
17
|
+ func login(data: Authentication) {
|
|
18
|
+
|
|
19
|
+ let service = "login.php"
|
|
20
|
+ guard let encodedData = try? JSONEncoder().encode(data)
|
|
21
|
+ else {
|
|
22
|
+ print("Failed to encode login request")
|
|
23
|
+ return
|
|
24
|
+ }
|
|
25
|
+
|
|
26
|
+ //print("JSON to send:", String(data: encodedData, encoding: .utf8)!)
|
|
27
|
+
|
|
28
|
+ let url = URL(string: serverAddress + service)!
|
|
29
|
+ var request = URLRequest(url: url)
|
|
30
|
+ request.setValue("applogin/json", forHTTPHeaderField: "Content-Type")
|
|
31
|
+ request.httpMethod = "POST"
|
|
32
|
+ request.httpBody = encodedData
|
|
33
|
+
|
|
34
|
+ URLSession.shared.dataTask(with: request) {data, response, error in
|
|
35
|
+
|
|
36
|
+ //handle result here
|
|
37
|
+ guard let data = data
|
|
38
|
+ else {
|
|
39
|
+ print("No data in response: \(error?.localizedDescription ?? "Unknown error").")
|
|
40
|
+ return
|
|
41
|
+ }
|
|
42
|
+ print(response ?? "No response")
|
|
43
|
+ print("Recieved data:", data)
|
|
44
|
+ print(error ?? "No error")
|
|
45
|
+
|
|
46
|
+ if let httpResponse = response as? HTTPURLResponse {
|
|
47
|
+ DispatchQueue.main.async {
|
|
48
|
+ if httpResponse.statusCode == 200 {
|
|
49
|
+ self.changeResponse(response: true)
|
|
50
|
+ } else {
|
|
51
|
+ self.changeResponse(response: false)
|
|
52
|
+ }
|
|
53
|
+
|
|
54
|
+ }
|
|
55
|
+ }
|
|
56
|
+
|
|
57
|
+ }.resume()
|
|
58
|
+ }
|
|
59
|
+}
|
|
60
|
+
|
10
|
61
|
struct LoginView: View {
|
11
|
62
|
@State var email: String = ""
|
12
|
63
|
@State var password: String = ""
|
13
|
64
|
@State var showingRegister: Bool = false
|
14
|
65
|
|
|
66
|
+ @ObservedObject var loginVM = LoginViewModel()
|
|
67
|
+
|
15
|
68
|
var body: some View {
|
16
|
69
|
VStack {
|
17
|
70
|
WelcomeImage()
|
18
|
71
|
WelcomeText()
|
19
|
72
|
|
20
|
|
- TextField("Email", text: $email)
|
21
|
|
- .autocapitalization(.none)
|
22
|
|
- .padding()
|
23
|
|
- .background(lightGreyColor)
|
24
|
|
- .cornerRadius(5.0)
|
25
|
|
- .padding(.bottom, 5)
|
26
|
|
- SecureField("Password", text: $password)
|
27
|
|
- .autocapitalization(.none)
|
28
|
|
- .padding()
|
29
|
|
- .background(lightGreyColor)
|
30
|
|
- .cornerRadius(5.0)
|
31
|
|
- .padding(.bottom, 20)
|
|
73
|
+ EmailField(email: $email)
|
|
74
|
+ PasswordField(password: $password)
|
|
75
|
+
|
|
76
|
+ if self.loginVM.response == false {
|
|
77
|
+ Text("Information incorrect, please try again")
|
|
78
|
+ .offset(y: -10)
|
|
79
|
+ .foregroundColor(.red)
|
|
80
|
+ } else if self.loginVM.response == true {
|
|
81
|
+ Text("Login successful 🎉")
|
|
82
|
+ .offset(y: -10)
|
|
83
|
+ .foregroundColor(.green)
|
|
84
|
+ }
|
32
|
85
|
|
33
|
|
- Button(action: {print("login tapped")}) {
|
|
86
|
+ Button(action: {
|
|
87
|
+ self.loginVM.login(data: Authentication(email: email, password: password))
|
|
88
|
+
|
|
89
|
+ }) {
|
34
|
90
|
LoginText()
|
35
|
91
|
}
|
36
|
92
|
|
37
|
93
|
Button(action: {self.showingRegister.toggle()}) {
|
38
|
94
|
RegisterText()
|
39
|
|
- }.sheet(isPresented: $showingRegister, content: {
|
40
|
|
- RegisterView()
|
41
|
|
- })
|
42
|
|
-
|
|
95
|
+ }
|
|
96
|
+ .sheet(isPresented: $showingRegister, content: {RegisterView()})
|
43
|
97
|
}
|
44
|
98
|
.padding()
|
45
|
99
|
}
|
|
@@ -96,3 +150,29 @@ struct RegisterText: View {
|
96
|
150
|
.cornerRadius(5)
|
97
|
151
|
}
|
98
|
152
|
}
|
|
153
|
+
|
|
154
|
+struct EmailField: View {
|
|
155
|
+ @Binding var email: String
|
|
156
|
+ @Environment(\.colorScheme) var colorScheme
|
|
157
|
+ var body: some View {
|
|
158
|
+ TextField("Email", text: $email)
|
|
159
|
+ .autocapitalization(.none)
|
|
160
|
+ .padding()
|
|
161
|
+ .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
|
|
162
|
+ .cornerRadius(5.0)
|
|
163
|
+ .padding(.bottom, 5)
|
|
164
|
+ }
|
|
165
|
+}
|
|
166
|
+
|
|
167
|
+struct PasswordField: View {
|
|
168
|
+ @Binding var password: String
|
|
169
|
+ @Environment(\.colorScheme) var colorScheme
|
|
170
|
+ var body: some View {
|
|
171
|
+ SecureField("Password", text: $password)
|
|
172
|
+ .autocapitalization(.none)
|
|
173
|
+ .padding()
|
|
174
|
+ .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
|
|
175
|
+ .cornerRadius(5.0)
|
|
176
|
+ .padding(.bottom, 20)
|
|
177
|
+ }
|
|
178
|
+}
|