// // AuthenticationView.swift // JobTrackerPro // // Created by Jake Steirer on 12/29/23. // import SwiftUI import Combine struct IdentifiableError: Identifiable { let id = UUID() let message: String } struct AuthView: View { @ObservedObject var viewModel: JobApplicationViewModel @State private var email: String = "" @State private var password: String = "" @State private var storedEmail: String = "" // Separate variable to hold the email @State private var storedPassword: String = "" // Separate variable to hold the password @State private var showingLogin = false @State private var showingCreateAccount = false @State private var identifiableError: IdentifiableError? @FocusState private var isInputActive: Bool // For iOS 15+ var body: some View { VStack { Image(systemName: "briefcase.fill") .resizable() .scaledToFit() .frame(width: 120, height: 120) .padding() Text("Welcome to Job Tracker Pro") .font(.largeTitle) TextField("Email", text: $email) .autocapitalization(.none) .keyboardType(.emailAddress) .disableAutocorrection(true) .padding() .border(Color.gray) .onChange(of: email) { [email] in storedEmail = email } .onSubmit { storedPassword = password // Store the password when submit the field } SecureField("Password", text: $password) .padding() .border(Color.gray) .onChange(of: password) { [password] in storedPassword = password } .onSubmit { storedPassword = password // Store the password when submit the field } Button("Log In") { hideKeyboard() // Dismiss the keyboard // Use storedEmail and storedPassword to log in // If you're handling login directly in AuthView viewModel.logIn(email: storedEmail, password: storedPassword) { success, message in // Handle completion } showingLogin = true } .padding() Button("Create Account") { hideKeyboard() // Dismiss the keyboard // Use storedEmail and storedPassword to create an account // If you're handling account creation directly in AuthView viewModel.createAccount(email: storedEmail, password: storedPassword) { success, message in // Handle completion } showingCreateAccount = true } .padding() } .alert(item: $identifiableError) { error in Alert( title: Text("Error"), message: Text(error.message), dismissButton: .default(Text("OK")) { identifiableError = nil } ) } .onReceive(viewModel.$error) { error in if let error = error { identifiableError = IdentifiableError(message: error.localizedDescription) } } } } #if canImport(UIKit) extension View { func hideKeyboard() { UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil) } } #endif