diff --git a/src/app/controllers/AuthController.php b/src/app/controllers/AuthController.php
new file mode 100644
index 0000000..a719352
--- /dev/null
+++ b/src/app/controllers/AuthController.php
@@ -0,0 +1,130 @@
+accountModel = new Account($dbConnection);
+ }
+
+ public function viewSignUp() {
+ $title = 'Sign Up';
+ require_once __DIR__ . '/../views/sign-up.php';
+ }
+
+ public function viewSignIn() {
+ $title = 'Sign In';
+ require_once __DIR__ . '/../views/sign-in.php';
+ }
+
+ public function viewAccount() {
+ if (!isset($_SESSION['user_id'])) {
+ header('Location: /sign-in');
+ exit;
+ }
+
+ $title = 'My account';
+ require_once __DIR__ . '/../views/account.php';
+ }
+
+ public function signUp() {
+ if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
+ header('Location: /sign-up');
+ exit;
+ }
+
+ $firstName = trim($_POST["first_name"] ?? '') ;
+ $lastName = trim($_POST["last_name"] ?? '');
+ $phone = !empty($_POST["phone"]) ? trim($_POST["phone"]) : null;
+ $email = trim($_POST["email"] ?? '');
+ $password = trim($_POST["password"] ?? '');
+ $conPassword = trim($_POST["confirm_password"] ?? '');
+
+ $errors = [];
+
+ if (empty($firstName) || empty($lastName) || empty($email) || empty($password) || empty($conPassword)) {
+ $_SESSION['errors'] = ["Please fill in all the fields."];
+ header('Location: /sign-up');
+ exit;
+ }
+
+ if ($this->accountModel->findByEmail($email)) {
+ $_SESSION['errors'] = ["Email already exists."];
+ header('Location: /sign-up');
+ exit;
+ }
+
+ if (!filter_var($email, FILTER_VALIDATE_EMAIL)) $errors[] = "Valid email is required.";
+ if (strlen($password) < 6) $errors[] = "Password must be at least 6 characters.";
+ if ($password !== $conPassword) $errors[] = "Passwords do not match.";
+
+ if (!empty($errors)) {
+ $_SESSION['errors'] = $errors;
+ header('Location: /sign-up');
+ exit;
+ }
+
+ if ($this->accountModel->create($firstName, $lastName, $email, $phone, $password)) {
+ $_SESSION['success'] = "Account created successfully!";
+ header('Location: /sign-in');
+ exit;
+ }
+
+ $_SESSION['errors'] = ["Error creating account!"];
+ header('Location: /sign-up');
+ exit;
+ }
+
+ public function signIn() {
+ if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
+ header('Location: /sign-in');
+ exit;
+ }
+
+ $email = trim($_POST['email'] ?? '');
+ $password = trim($_POST['password'] ?? '');
+
+ $user = $this->accountModel->findByEmail($email);
+
+ if ($user && password_verify($password, $user['password'])) {
+ $_SESSION['user_id'] = $user['id'];
+ $_SESSION['user_first_name'] = $user['first_name'];
+ $_SESSION['user_last_name'] = $user['last_name'];
+ $_SESSION['user_email'] = $user['email'];
+ header('Location: /account');
+ exit;
+ }
+
+ $_SESSION['errors'] = ["Invalid email or password."];
+ header('Location: /sign-in');
+ exit;
+ }
+
+ public function signOut() {
+ $_SESSION = [];
+ session_destroy();
+ header('Location: /');
+ exit;
+ }
+
+ public function getAccountData() {
+ header('Content-Type: application/json');
+
+ if (!isset($_SESSION['user_id'])) {
+ http_response_code(401);
+ echo json_encode(['error' => 'Access denied.']);
+ exit;
+ }
+
+ $data = [
+ 'first_name' => $_SESSION['user_first_name'] ?? '',
+ 'last_name' => $_SESSION['user_last_name'] ?? '',
+ 'email' => $_SESSION['user_email'] ?? ''
+ ];
+
+ echo json_encode($data);
+ exit;
+ }
+}
\ No newline at end of file
diff --git a/src/app/controllers/HomeController.php b/src/app/controllers/HomeController.php
index 5247f8e..807a910 100644
--- a/src/app/controllers/HomeController.php
+++ b/src/app/controllers/HomeController.php
@@ -3,16 +3,4 @@ class HomeController {
public function home() {
require_once __DIR__ . '/../views/home.php';
}
-
- public function viewSignIn() {
- require_once __DIR__ . '/../views/sign-in.php';
- }
-
- public function viewSignUp() {
- require_once __DIR__ . '/../views/sign-up.php';
- }
-
- public function viewAccount() {
- require_once __DIR__ . '/../views/account_view.php';
- }
}
\ No newline at end of file
diff --git a/src/app/controllers/TestController.php b/src/app/controllers/TestController.php
deleted file mode 100644
index 637c48a..0000000
--- a/src/app/controllers/TestController.php
+++ /dev/null
@@ -1,16 +0,0 @@
-prepare('INSERT INTO account (first_name, last_name, phone, email, password, date_creation, role) VALUES (?, ?, ?, ?, ?, ?, ?)');
-$query->execute([$first_name, $last_name, $phone, $email, password_hash($password, PASSWORD_DEFAULT), date("Y-m-d"), 1]);
-
-header('Location: /sign-in');
\ No newline at end of file
diff --git a/src/app/models/Account.php b/src/app/models/Account.php
new file mode 100644
index 0000000..108c4c9
--- /dev/null
+++ b/src/app/models/Account.php
@@ -0,0 +1,34 @@
+conn = $dbConnection;
+ }
+
+ public function create($firstName, $lastName, $email, $phone, $password) {
+ $hashedPassword = password_hash($password, PASSWORD_DEFAULT);
+
+ $query = "INSERT INTO account (first_name, last_name, email, phone, password)
+ VALUES (:firstName, :lastName, :email, :phone, :password)";
+
+ $stmt = $this->conn->prepare($query);
+ $stmt->bindValue(':firstName', $firstName);
+ $stmt->bindValue(':lastName', $lastName);
+ $stmt->bindValue(':email', $email);
+ $stmt->bindValue(':phone', $phone);
+ $stmt->bindValue(':password', $hashedPassword);
+
+ return $stmt->execute();
+ }
+
+ public function findByEmail($email) {
+ $query = "SELECT * FROM account WHERE email = :email";
+
+ $stmt = $this->conn->prepare($query);
+ $stmt->bindValue(':email', $email);
+ $stmt->execute();
+
+ return $stmt->fetch(PDO::FETCH_ASSOC);
+ }
+}
\ No newline at end of file
diff --git a/src/app/models/TestModel.php b/src/app/models/TestModel.php
deleted file mode 100644
index 57545fd..0000000
--- a/src/app/models/TestModel.php
+++ /dev/null
@@ -1,19 +0,0 @@
-conn = $database->getConnection();
- }
-
- public function getStatus() {
- if ($this->conn) {
- echo "Database connection: Success";
- } else {
- echo "Error with connection";
- }
- }
-}
\ No newline at end of file
diff --git a/src/app/models/account.php b/src/app/models/account.php
deleted file mode 100644
index fa43067..0000000
--- a/src/app/models/account.php
+++ /dev/null
@@ -1,15 +0,0 @@
-
diff --git a/src/app/views/account.php b/src/app/views/account.php
new file mode 100644
index 0000000..24d676b
--- /dev/null
+++ b/src/app/views/account.php
@@ -0,0 +1,53 @@
+
+
+My account
+
+
= $account->first_name ?>
-= $account->last_name ?>
-= $account->email ?>
-= $account->phone ?>
-= $account->date_creation ?>
-