From 9ad19bb4d694f3683aa77e02cba9e3eec33dbaec Mon Sep 17 00:00:00 2001
From: Bitha17 <16521076@mahasiswa.itb.ac.id>
Date: Wed, 4 Oct 2023 23:13:52 +0700
Subject: [PATCH] integrate login and register fe and be

---
 app/Controllers/UserController.php | 35 ++++++++++++++++++++++++++----
 app/Models/User.php                | 16 ++++++++++----
 app/Views/login/register.php       |  2 +-
 3 files changed, 44 insertions(+), 9 deletions(-)

diff --git a/app/Controllers/UserController.php b/app/Controllers/UserController.php
index c4f59a2..dd67e56 100644
--- a/app/Controllers/UserController.php
+++ b/app/Controllers/UserController.php
@@ -1,6 +1,6 @@
 <?php
 // app/controllers/userController.php
-
+ob_start();
 require_once(__DIR__ . '/../Models/User.php');
 
 
@@ -72,9 +72,36 @@ class UserController {
                 $emailOrUsername = $_POST['loginIdentifier'];
                 $password = $_POST['loginPassword'];
                 if (filter_var($emailOrUsername, FILTER_VALIDATE_EMAIL)){
-                    echo $this->loginByEmail($emailOrUsername, $password);
+                    if ($this->loginByEmail($emailOrUsername, $password) === "success") {
+                        header("Location:/../../../home.php");
+                        ob_end_flush();
+                    } else {
+                        header("Location:Views/login/login.php");
+                        ob_end_flush();
+                    }
+                } else {
+                    if ($this->loginByUsername($emailOrUsername,$password) === "success") {
+                        header("Location:/../../../home.php");
+                        ob_end_flush();
+                    } else {
+                        header("Location:Views/login/login.php");
+                        ob_end_flush();
+                    }
+                }
+            } elseif ($_POST['userAction'] === 'createUser') {
+                unset($_POST['userAction']);
+                $userName = $_POST['userName'];
+                $userUsername = $_POST['userUsername'];
+                $userEmail = $_POST['userEmail'];
+                $userPassword = $_POST['userPassword'];
+                $isAdmin = isset($_POST['isAdmin']) ? 1 : 0;
+                $success = $this->registerUser($userName,$userUsername,$userEmail,$userPassword,$isAdmin)['success'];
+                if($success){
+                    header("Location:Views/login/login.php");
+                    ob_end_flush();
                 } else {
-                    echo $this->loginByUsername($emailOrUsername,$password);
+                    header("Location:Views/login/register.php");
+                    ob_end_flush();
                 }
             } else {
                 // Handle other actions here, if needed
@@ -83,4 +110,4 @@ class UserController {
     }
 
 }
-?>
+?>
\ No newline at end of file
diff --git a/app/Models/User.php b/app/Models/User.php
index 189678f..dc6079a 100644
--- a/app/Models/User.php
+++ b/app/Models/User.php
@@ -7,20 +7,28 @@ require_once(__DIR__ . '/../../db/connect.php');
 class UserModel {
     public function createUser($name, $username, $email, $hashedPass, $isAdmin) {
         global $db;
+
+        $response = [
+            'success'=> true,
+            'message'=> ''
+        ];
     
         try {
             $stmt = $db->prepare("INSERT INTO users (user_name, username, user_email, user_hashedPass, isAdmin) VALUES (?, ?, ?, ?, ?)");
             $stmt->execute([$name, $username, $email, $hashedPass, $isAdmin]);
-            
-            return "User created successfully";
+            $response['message'] = "User created successfully";
+            return $response;
         } catch (PDOException $e) {
+            $response['success'] = false;
             // Check if the error code corresponds to a unique constraint violation.
             if ($e->getCode() === '23000') {
                 // Handle the error as a duplicate entry.
-                return "Username or email already exists. Please choose a different one.";
+                $response['message'] = "Username or email already exists. Please choose a different one.";
+                return $response;
             } else {
                 // Handle other database errors.
-                return "Database error: " . $e->getMessage();
+                $response['message'] = "Database error: " . $e->getMessage();
+                return $response;
             }
         }
     }
diff --git a/app/Views/login/register.php b/app/Views/login/register.php
index f326b6d..d6a5772 100644
--- a/app/Views/login/register.php
+++ b/app/Views/login/register.php
@@ -11,7 +11,7 @@
     <body>
         <div class="register-container">
             <h1>Register</h1>
-            <form id="registerForm" method="post" action="../../../home.php">
+            <form id="registerForm" method="post" action="../../router.php">
                 <label for="userName">Name</label>
                 <input type="text" id="userName" name="userName" required>
 
-- 
GitLab