Skip to content

Commit

Permalink
adding error message pop-up in LoginView
Browse files Browse the repository at this point in the history
  • Loading branch information
TracyL5982 committed Nov 11, 2023
1 parent f0df9fb commit 60a8c34
Showing 1 changed file with 83 additions and 45 deletions.
128 changes: 83 additions & 45 deletions app/newHere1/newHere/LoginView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ struct LoginView: View {
@State private var isRegistered = false
@Binding var isAuthenticated: Bool

@State private var showingAlert = false
@State private var alertMessage = ""

var body: some View {

NavigationView {
Expand All @@ -29,7 +32,7 @@ struct LoginView: View {
.background(Color(.systemGray6))
.cornerRadius(5.0)
.padding(.bottom, 20)

SecureField("Password", text: $password)
.padding()
.background(Color(.systemGray6))
Expand All @@ -46,7 +49,7 @@ struct LoginView: View {
.cornerRadius(5.0)
}
.padding(.horizontal)

NavigationLink(destination: RegistrationView(isRegistered: $isRegistered)) {
Text("Don't have an account? Signup")
}
Expand All @@ -55,61 +58,96 @@ struct LoginView: View {
Spacer()
}
.padding()
.alert(isPresented: $showingAlert) {
Alert(title: Text("Login Status"), message: Text(alertMessage), dismissButton: .default(Text("OK")))
}
}
}

func LogInUser(){
print("Login user called")
let requestBody: [String: Any] = [
"inputLogin": username,
"password": password]

guard let jsonData = try? JSONSerialization.data(withJSONObject: requestBody)
else{
return
}

guard let url = URL(string: loginUrlString) else {
return
}

var request = URLRequest(url: url)
request.httpMethod = "POST"
request.httpBody = jsonData
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.setValue(logApiKey, forHTTPHeaderField: "x-api-key")

URLSession.shared.dataTask(with: request) { data, response, error in
if let error = error{
print("error:\(error)")
func LogInUser(){
if username.isEmpty || password.isEmpty {
self.alertMessage = "Please enter both username and password."
self.showingAlert = true
return
}

print("Login user called")
let requestBody: [String: Any] = [
"inputLogin": username,
"password": password]

guard let jsonData = try? JSONSerialization.data(withJSONObject: requestBody)
else{
return
}
guard let httpResponse = response as? HTTPURLResponse else {
print("Invalid HTTP Response")

guard let url = URL(string: loginUrlString) else {
return
}
let statusCode = httpResponse.statusCode

if statusCode == 200 {
if let data = data {
if let responseString = String(data: data, encoding: .utf8) {
print("Login Response: \(responseString)")
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.httpBody = jsonData
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.setValue(logApiKey, forHTTPHeaderField: "x-api-key")

URLSession.shared.dataTask(with: request) { data, response, error in
DispatchQueue.main.async {
if let error = error{
self.alertMessage = "Login failed: \(error.localizedDescription)"
self.showingAlert = true
}else if let httpResponse = response as? HTTPURLResponse{
let statusCode = httpResponse.statusCode
if statusCode == 200 {
if let data = data {
if let responseString = String(data: data, encoding: .utf8) {
print("Login Response: \(responseString)")
}
self.isAuthenticated = true;
}
}else if statusCode == 404 {
self.alertMessage = "User not found. Please check your credentials."
self.showingAlert = true
}else{
self.alertMessage = "Login failed: Server returned status code \(httpResponse.statusCode)"
self.showingAlert = true
}
}else{
// General error
self.alertMessage = "Login failed: Unexpected error occurred"
self.showingAlert = true
}
self.isAuthenticated = true;
}
}
// else got error from server
else {
if let response = response {
print("response:\(response)")
}
}

// guard let httpResponse = response as? HTTPURLResponse else {
// self.alertMessage = "Login failed: Server returned status code \(httpResponse.statusCode)"
// self.showingAlert = true
//// print("Invalid HTTP Response")
// return
// }
//
// let statusCode = httpResponse.statusCode
//
// if statusCode == 200 {
// if let data = data {
// if let responseString = String(data: data, encoding: .utf8) {
// print("Login Response: \(responseString)")
// }
// self.isAuthenticated = true;
// }
// }
// // else got error from server
// else {
// if let response = response {
// print("response:\(response)")
// }
// }

}.resume()

}.resume()
}

}

}

//// Preview Provider
//struct LoginView_Previews: PreviewProvider {
// static var previews: some View {
Expand Down

0 comments on commit 60a8c34

Please sign in to comment.