I've been using php and a mysql database to create a simple user registration and login system. the login php files work, and I have connected it to my project in Xcode. however, I'm not too sure on how to make sure that this function runs when the login button is clicked, and only moves to the next view when the log in has been successful.
This is my swift code :
import SwiftUI
struct loginview_Previews: PreviewProvider {
static var previews: some View {
loginview()
}
}
struct loginview: View {
// variables and fields
@State private var username : String = ""
@State private var password : String = ""
let databaseurl = "http://localhost/login.php"
var body: some View {
// put into a navigation view
NavigationView{
// form to enter details
Form{
// start section with a header for info
Section(header: Text("enter your details to log in !")){
// username field
TextField("username", text: $username)
.font(.headline)
.frame(width: 350.0)
.foregroundColor(.white)
.padding(.all, 20.0)
.cornerRadius(9.0)
.preferredColorScheme(.dark)
TextField("password", text: $password)
.font(.headline)
.frame(width: 350.0)
.foregroundColor(.white)
.padding(.all, 20.0)
.cornerRadius(9.0)
.preferredColorScheme(.dark)
} .textCase(nil) // make header lowercase
Section{
NavigationLink(destination: homepage()){ // link to home page view
Text("submit")
.foregroundColor(.white)
.fontWeight(.heavy)
.font(.system(size: 22))
.padding(.all, 20.0)
.frame(width: 175.0)
.preferredColorScheme(.dark)
}
}
// button is unclickable if textfields are empty
.disabled(username.isEmpty)
.disabled(password.isEmpty)
}
.navigationBarTitle("log in") // title of form
}
}
//
func logindatabase(){
// create NSURL - an object initialized with URLString
if var requesturl = URLComponents(string: databaseurl) {
requesturl.query = "username=\(username)&password=\(password)"
print(requesturl)
guard let url = requesturl.url else { return }
let task = URLSession.shared.dataTask(with: url) { data, response, error in
if error != nil {
print("error is \(String(describing: error))")
return
}
// parse the response
do {
// convert response to NSDictionary
let myJSON = try JSONSerialization.jsonObject(with: data! , options: .mutableContainers) as? NSDictionary
if let parseJSON = myJSON {
// create a string & get the json response
guard let message = parseJSON["message"] as? String else { return }
// print the response
print(message)
}
} catch {
print(error)
}
}
task.resume()
}
}
}
and this is my login.php file :
<?php
//include the db operation file
require_once 'dboperations.php' ;
// create a response array
$response = array();
if ($_SERVER['REQUEST_METHOD'] == 'GET') {
if (isset($_GET['username']) && isset($_GET['password'])) {
$db = new dboperation();
if ($db->userlogin($_GET['username'], $_GET['password'])) {
$response['error'] = false;
$response['user'] = $db->displayuser($_GET['username']);
} else {
$response['error'] = true;
$response['message'] = 'invalid username or password';
}
} else {
$response['error'] = true;
$response['message'] = 'parameters are missing';
}
} else {
$response['error'] = true;
$response['message'] = "Request not allowed";
}
echo json_encode($response);
which does work successfully.