Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix:errors #34

Merged
merged 1 commit into from
Oct 30, 2023
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
1 change: 1 addition & 0 deletions app/controllers/Logout.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class Logout {
public function index(): void {
$data['email'] = empty($_SESSION['USER']) ? '' : $_SESSION['USER']->email;

setcookie('jwt_auth_token', '', time() - 1, '/');
session_destroy();
redirect('home');
}
Expand Down
10 changes: 9 additions & 1 deletion app/core/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,17 @@ public function loadController(): void {
// }
// }
// $this->runMiddleware();
$user = AuthMiddleware::run_middleware($this->controller, $this->method);
$user = UserMiddleware::user(AuthMiddleware::run_middleware($this->controller, $this->method));
// make user global to all views

$_SESSION['USER'] = $user;
// show($user);
// show($_SESSION['USER']);


// show($user);
call_user_func_array([$controller, $this->method], $URL);

}
}

Expand Down
1 change: 1 addition & 0 deletions app/core/init.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@
require 'Controller.php';
require 'App.php';
require '../app/middlewares/AuthMiddleware.php';
require '../app/middlewares/UserMiddleware.php';
3 changes: 3 additions & 0 deletions app/middlewares/AuthMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public static function run_middleware(string $controller, string $method): mixed
$authRequired = [
'Home' => ['index', 'method2'],
'Controller2' => ['method3'],
// 'Profile' => ['index', 'edit', 'update'],
];
$unauthRequired = [
'Login' => ['index'],
Expand All @@ -40,6 +41,8 @@ public static function run_middleware(string $controller, string $method): mixed
if (isset($unauthRequired[$currentController]) &&
in_array($method, $unauthRequired[$currentController])) {
Self::not_authenticated();
}else {
Self::check();
}

// return Self::$user;
Expand Down
47 changes: 47 additions & 0 deletions app/middlewares/UserMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

class UserMiddleware {

static function user($user){
if (!$user || !(isset($user['role']))) {
return false;
}
// if ()

if ($user['role'] == 'customer') {
$customer = new CustomerModel;
$customer = $customer->first([
'user_id'=> $user['id']
]);
// echo $customer;
$user = (object) array_merge((array) $user, (array) $customer);
return $user;
}
if ($user['role'] == 'guide') {
$guide = new GuideModel;
$guide = $guide->first([
'user_id'=> $user['id']
]);
// show($d);
$user = (object) array_merge((array) $user, (array) $guide);
return $user;
}
if ($user['role'] == 'rental_service') {
$rental_service = new RentalServiceModel;
$rental_service = $rental_service->first([
'user_id'=> $user['id']
]);
$user = (object) array_merge((array) $user, (array) $rental_service);
return $user;
}
}

}







?>
1 change: 1 addition & 0 deletions app/models/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class CustomerModel {
'address',
'number',
'nic',
'user_id'
];

public function registerCustomer(array $data){
Expand Down
4 changes: 3 additions & 1 deletion app/views/admin/components/navbar.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@
<li class="nav-menu-item"><a href="#">Tips & Knowhows</a></li>
<li class="nav-menu-item"><a href="#">Complaints</a></li>
</ul>
<div class="login-button"><a href="#">Login</a></div>

<div class="login-button"><a href="<?= ROOT_DIR ?>/login">Login</a></div>

</nav>
56 changes: 54 additions & 2 deletions app/views/components/navbar.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,57 @@
<li class="nav-menu-item"><a href="#">Tips & Knowhows</a></li>
<li class="nav-menu-item"><a href="#">Complaints</a></li>
</ul>
<div class="login-button"><a href="#">Login</a></div>
</nav>
<?php if (isset($_SESSION['USER'])){ $user = $_SESSION['USER']; ?>

<!-- profile avatar with dropdown -->

<div class="profile-avatar">
<img src="<?php echo ROOT_DIR?>/assets/images/1.png" alt="profile picture">
<div class="dropdown-menu" id="nav-dropdown">
<ul>
<li><a href="<?= ROOT_DIR ?>/profile">Profile</a></li>
<li><a href="<?= ROOT_DIR ?>/settings">Settings</a></li>
<li><a href="<?= ROOT_DIR ?>/logout">Logout</a></li>
</ul>
</div>
</div>


<?php }else { ?>
<div class="login-button"><a href="<?= ROOT_DIR ?>/login">Login</a></div>

<?php } ?>
</nav>

<script>
// Get the profile avatar element and the dropdown menu
var profileAvatar = document.querySelector('.profile-avatar');
var dropdownMenu = document.getElementById('nav-dropdown');

// Toggle the dropdown menu when clicking on the profile avatar
profileAvatar.addEventListener('click', function(event) {
// Prevent the default behavior of the anchor tags
event.preventDefault();

// Toggle the display style of the dropdown menu
if (dropdownMenu.style.display === 'block') {
dropdownMenu.style.display = 'none';
} else {
dropdownMenu.style.display = 'block';
}
});

// Close the dropdown menu if the user clicks outside of it
document.addEventListener('click', function(event) {
if (!profileAvatar.contains(event.target) && !dropdownMenu.contains(event.target)) {
dropdownMenu.style.display = 'none';
}
});

// Prevent event propagation when clicking on dropdown links
dropdownMenu.addEventListener('click', function(event) {
event.stopPropagation();
});


</script>
6 changes: 3 additions & 3 deletions app/views/profile.view.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

<div class="div-1">
<div class="div-12">
<div class="text-wrapper">Hello Jenny!</div>
<!-- <div class="text-wrapper">Hello Jenny!</div> -->
<div class="img-1">
<img src="<?php echo ROOT_DIR?>/assets/images/1.png" alt="">
</div>
Expand All @@ -26,10 +26,10 @@

<div class="div-4">
<div class="div-wrapper">
<div class="text-wrapper-2">Name : Jenny Fernando</div>
<div class="text-wrapper-2">Name : <?php echo $user->name ?></div>
</div>
<div class="div-wrapper">
<div class="text-wrapper-2">Age : 20</div>
<div class="text-wrapper-2">NIC : <?php echo $user->nic ?></div>
</div>
<div class="div-wrapper">
<div class="text-wrapper-2">Role : Customer</div>
Expand Down
66 changes: 66 additions & 0 deletions public/assets/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,72 @@
color: #4C5039;
}



/* nav profile avatar*/


.navbar .profile-avatar {
position: relative;
display: inline-block;
margin-right: 20px;
}

.navbar .profile-avatar img {
width: 40px;
height: 40px;
border-radius: 50%;
cursor: pointer;
}

.navbar .dropdown-menu {
display: none;
position: absolute;
top: 60px;
right: 0;
background-color: #fff; /* Set the default background color */
color: #4c5039; /* Set the default font color */
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
z-index: 2;
}

.navbar .dropdown-menu ul {
list-style: none;
padding: 0;
margin: 0;
}
/*
.navbar .dropdown-menu ul li {
padding: 10px;
} */

/* Style the links inside the dropdown menu */
.navbar .dropdown-menu ul li a {
text-decoration: none;
color: #4c5039; /* Set default link color */
transition: all 0.3s ease; /* Add smooth transition effect for all properties */
display: block;
padding: 10px 20px ;
}

/* Change link color and background on hover */
.navbar .dropdown-menu ul li a:hover {
color: #6b6f55; /* Set a different color on hover */
background-color: #f0f0f0; /* Change background color on hover */
}

/* Show dropdown menu on click */
.navbar .profile-avatar.active .dropdown-menu {
display: block;
}





/* nav profile end */


.section-button {
background-color: #F4F4F4;
color: #4C5039;
Expand Down