Skip to content
This repository was archived by the owner on Jul 20, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 130 additions & 0 deletions src/app/controllers/AuthController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php

require_once __DIR__ . '/../models/Account.php';

class AuthController {
private $accountModel;

public function __construct(PDO $dbConnection) {
$this->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;
}
}
12 changes: 0 additions & 12 deletions src/app/controllers/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
}
16 changes: 0 additions & 16 deletions src/app/controllers/TestController.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/app/controllers/sign-in.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

require __DIR__ .'/../models/account.php';
require __DIR__ . '/../models/Account.php';
require __DIR__ .'/../../config/database.php';

$email = $_REQUEST["email"];
Expand Down
25 changes: 0 additions & 25 deletions src/app/controllers/sign-up.php

This file was deleted.

34 changes: 34 additions & 0 deletions src/app/models/Account.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
class Account {
private $conn;

public function __construct(PDO $dbConnection) {
$this->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);
}
}
19 changes: 0 additions & 19 deletions src/app/models/TestModel.php

This file was deleted.

15 changes: 0 additions & 15 deletions src/app/models/account.php

This file was deleted.

53 changes: 53 additions & 0 deletions src/app/views/account.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php include 'partials/header.php'; ?>

<main>
<h1>My account</h1>

<div id="loading">Loading account information...</div>
<!-- Sensitive information are not displayed until server is sure user is authenticated -->
<div id="account-content" style="display: none;">
<div class="account-details">
<div>
<label>First name</label>
<p id="user-first-name"></p>
</div>
<div>
<label>Last name</label>
<p id="user-last-name"></p>
</div>
<div>
<label>Email</label>
<p id="user-email"></p>
</div>
</div>
</div>
</main>

<script>
fetch('/account-data')
.then(response => {
// Session has been lost
if (response.status === 401) {
window.location.href = '/sign-in';
return;
}

return response.json();
})
.then(data => {
if (data && !data.error) {
document.getElementById('user-first-name').textContent = data.first_name;
document.getElementById('user-last-name').textContent = data.last_name;
document.getElementById('user-email').textContent = data.email;

document.getElementById('loading').style.display = 'none';
document.getElementById('account-content').style.display = 'block';
}
})
.catch(err => {
// Redirect if network issue
window.location.href = '/sign-in';
});
</script>

<?php include 'partials/footer.php'; ?>
30 changes: 0 additions & 30 deletions src/app/views/account_view.php

This file was deleted.

5 changes: 4 additions & 1 deletion src/app/views/partials/footer.php
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
</body>
</body>
<footer>
This is a <strong>F O O T E R</strong>
</footer>
</html>
11 changes: 6 additions & 5 deletions src/app/views/partials/header.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<html lang="fr">
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<title><?php echo isset($title) ? $title : "EFES KEBAB"; ?></title></head>
<title><?php echo isset($title) ? $title : "EFES KEBAB"; ?></title>
</head>

<body>
<?php include 'navbar.php';
// [FIXME] Or might be also 'nav.php'
?>
<?php include 'navbar.php'; ?>
14 changes: 0 additions & 14 deletions src/app/views/partials/nav.php

This file was deleted.

Loading