123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- //
- // LogIn.swift
- // Comedores Sociales
- //
- // Created by Hector Carrion on 10/24/20.
- //
-
- import SwiftUI
-
- class LoginViewModel: ObservableObject {
- @Published var response: Bool?
-
- func changeResponse(response: Bool) {
- self.response = response
- }
-
- func login(data: Authentication) {
-
- let service = "login.php"
- guard let encodedData = try? JSONEncoder().encode(data)
- else {
- print("Failed to encode login request")
- return
- }
-
- //print("JSON to send:", String(data: encodedData, encoding: .utf8)!)
-
- let url = URL(string: serverAddress + service)!
- var request = URLRequest(url: url)
- request.setValue("applogin/json", forHTTPHeaderField: "Content-Type")
- request.httpMethod = "POST"
- request.httpBody = encodedData
-
- URLSession.shared.dataTask(with: request) {data, response, error in
-
- //handle result here
- guard let data = data
- else {
- print("No data in response: \(error?.localizedDescription ?? "Unknown error").")
- return
- }
- print(response ?? "No response")
- print("Recieved data:", data)
- print(error ?? "No error")
-
- if let httpResponse = response as? HTTPURLResponse {
- DispatchQueue.main.async {
- if httpResponse.statusCode == 200 {
- self.changeResponse(response: true)
- } else {
- self.changeResponse(response: false)
- }
-
- }
- }
-
- }.resume()
- }
- }
-
- struct LoginView: View {
- @State var email: String = ""
- @State var password: String = ""
- @State var showingRegister: Bool = false
-
- @ObservedObject var loginVM = LoginViewModel()
-
- var body: some View {
- VStack {
- WelcomeImage()
- WelcomeText()
-
- EmailField(email: $email)
- PasswordField(password: $password)
-
- if self.loginVM.response == false {
- Text("Information incorrect, please try again")
- .offset(y: -10)
- .foregroundColor(.red)
- } else if self.loginVM.response == true {
- Text("Login successful 🎉")
- .offset(y: -10)
- .foregroundColor(.green)
- }
-
- Button(action: {
- self.loginVM.login(data: Authentication(email: email, password: password))
-
- }) {
- LoginText()
- }
-
- Button(action: {self.showingRegister.toggle()}) {
- RegisterText()
- }
- .sheet(isPresented: $showingRegister, content: {RegisterView()})
- }
- .padding()
- }
- }
-
- struct LogInView_Previews: PreviewProvider {
- static var previews: some View {
- LoginView()
- }
- }
-
- struct WelcomeText: View {
- var body: some View {
- Text("Comedores Sociales")
- .font(.largeTitle)
- .fontWeight(.semibold)
- .padding(.bottom, 20)
- }
- }
-
- struct WelcomeImage: View {
- var body: some View {
- Image("logInImage")
- .resizable()
- .aspectRatio(contentMode: .fill)
- .frame(width: 150, height: 150)
- .clipped()
- //.cornerRadius(150)
- //.padding(.bottom, 75)
- }
- }
-
- struct LoginText: View {
- var body: some View {
- Text("Login")
- .font(.headline)
- .foregroundColor(.white)
- .padding()
- .frame(width: 220, height: 50)
- .background(Color.blue)
- .cornerRadius(5)
- .padding(.bottom, 5)
- }
- }
-
- struct RegisterText: View {
- var body: some View {
- Text("Don't have an account? Register")
- .font(.headline)
- .foregroundColor(.gray)
- .padding()
- .frame(width: 350, height: 50)
- .background(Color.clear)
- .cornerRadius(5)
- }
- }
-
- struct EmailField: View {
- @Binding var email: String
- @Environment(\.colorScheme) var colorScheme
- var body: some View {
- TextField("Email", text: $email)
- .autocapitalization(.none)
- .padding()
- .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
- .cornerRadius(5.0)
- .padding(.bottom, 5)
- }
- }
-
- struct PasswordField: View {
- @Binding var password: String
- @Environment(\.colorScheme) var colorScheme
- var body: some View {
- SecureField("Password", text: $password)
- .autocapitalization(.none)
- .padding()
- .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
- .cornerRadius(5.0)
- .padding(.bottom, 20)
- }
- }
|