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

Update login and register pages #40

Merged
merged 1 commit into from
Sep 29, 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
27 changes: 25 additions & 2 deletions app/controllers/Users.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ public function register(){
'name' => trim($_POST['name']),
'email' => trim($_POST['email']),
'password' => trim($_POST['password']),
'username' => trim($_POST['username']),
'confirm_password' => trim($_POST['confirm_password']),
'user_type' => trim($_POST['user_type']),
'contact_no' => trim($_POST['contact_no']),
'err' => ''
];

Expand All @@ -35,6 +37,16 @@ public function register(){
}
}

// Validate username
if (empty($data['username'])){
$data['err'] = 'Please enter username';
} else {
// Check email
if ($this->userModel->findUserByUsername($data['username'])){
$data['err'] = 'Username is already taken';
}
}

// Validate password
if (empty($data['password'])){
$data['err'] = 'Please enter password';
Expand All @@ -56,6 +68,11 @@ public function register(){
$data['err'] = 'Please select user type';
}

// Validate contact number
if (empty($data['contact_no'])){
$data['err'] = 'Please enter contact number';
}

// Validation is completed and no error found
if (empty($data['err'])){
// Hash password
Expand All @@ -77,9 +94,11 @@ public function register(){
$data = [
'name' => '',
'email' => '',
'username' => '',
'password' => '',
'confirm_password' => '',
'user_type' => '',
'contact_no' => '',
'err' => '',
];

Expand All @@ -97,6 +116,7 @@ public function login(){
// Input data
$data = [
'email' => trim($_POST['email']),
'username' => trim($_POST['email']),
'password' => trim($_POST['password']),
'remember_me' => isset($_POST['remember_me']),
'err' => ''
Expand All @@ -108,7 +128,7 @@ public function login(){
$data['err'] = 'Please enter email';
}
else{
if ($this->userModel->findUserByEmail($data['email'])){
if ($this->userModel->findUserByEmail($data['email']) or $this->userModel->findUserByUsername($data['username'])){
// User found
}
else{
Expand All @@ -125,7 +145,7 @@ public function login(){
// Check if error is empty
if (empty($data['err'])){
// log the user
$loggedInUser = $this->userModel->login($data['email'], $data['password']);
$loggedInUser = $this->userModel->login($data['email'], $data['password'], $data['username']);
if ($loggedInUser){
// Create session
$this->createUserSession($loggedInUser);
Expand All @@ -151,6 +171,7 @@ public function login(){
// Initial form load
$data = [
'email' => '',
'username' => '',
'password' => '',
'err' => ''
];
Expand All @@ -176,6 +197,7 @@ private function setRememberMeCookie($userId) {
public function createUserSession($user){
$_SESSION['user_id'] = $user->id;
$_SESSION['user_email'] = $user->email;
$_SESSION['username'] = $user->username;
$_SESSION['user_name'] = $user->name;
$_SESSION['user_type'] = $user->userType;

Expand All @@ -186,6 +208,7 @@ public function createUserSession($user){
public function logout(){
unset($_SESSION['user_id']);
unset($_SESSION['user_email']);
unset($_SESSION['username']);
unset($_SESSION['user_name']);
unset($_SESSION['user_type']);

Expand Down
70 changes: 67 additions & 3 deletions app/models/DriverModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,73 @@ public function __construct(){
$this->db = new Database();
}

public function getUser(){
$this->db->query('SELECT * FROM users');
// Register user
public function register($data): bool
{
// Prepare statement
$this->db->query('INSERT INTO user (name, username, email, password, userType, contactNo) VALUES (:name, :username, :email, :password, :userType, :contactNo)');

return $this->db->resultSet();
// Bind values
$this->db->bind(':name', $data['name']);
$this->db->bind(':email', $data['email']);
$this->db->bind(':username', $data['username']);
$this->db->bind(':password', $data['password']);
$this->db->bind(':userType', $data['user_type']);
$this->db->bind(':contactNo', $data['contact_no']);

// Execute
if ($this->db->execute()){
return true;
}
else {
return false;
}
}

// Find user
public function findUserByEmail($email): bool
{
$this->db->query('SELECT * FROM user WHERE email = :email');
$this->db->bind(':email', $email);

$row = $this->db->single();

// Check row
if ($this->db->rowCount() > 0){
return true;
} else {
return false;
}
}

public function findUserByUsername($username): bool
{
$this->db->query('SELECT * FROM user WHERE username = :username');
$this->db->bind(':username', $username);

$row = $this->db->single();

// Check row
if ($this->db->rowCount() > 0){
return true;
} else {
return false;
}
}

// Login user
public function login($email, $password, $username){
$this->db->query('SELECT * FROM user WHERE email = :email OR username = :username');
$this->db->bind(':email', $email);
$this->db->bind(':username', $username);

$row = $this->db->single();

$hashed_password = $row->password;
if (password_verify($password, $hashed_password)){
return $row;
} else {
return false;
}
}
}
32 changes: 26 additions & 6 deletions app/models/UserModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@ public function __construct(){
}

// Register user
public function register($data){
public function register($data): bool
{
// Prepare statement
$this->db->query('INSERT INTO users (name, email, password, userType) VALUES (:name, :email, :password, :userType)');
$this->db->query('INSERT INTO user (name, username, email, password, userType, contactNo) VALUES (:name, :username, :email, :password, :userType, :contactNo)');

// Bind values
$this->db->bind(':name', $data['name']);
$this->db->bind(':email', $data['email']);
$this->db->bind(':username', $data['username']);
$this->db->bind(':password', $data['password']);
$this->db->bind(':userType', $data['user_type']);
$this->db->bind(':contactNo', $data['contact_no']);

// Execute
if ($this->db->execute()){
Expand All @@ -27,8 +30,9 @@ public function register($data){
}

// Find user
public function findUserByEmail($email){
$this->db->query('SELECT * FROM users WHERE email = :email');
public function findUserByEmail($email): bool
{
$this->db->query('SELECT * FROM user WHERE email = :email');
$this->db->bind(':email', $email);

$row = $this->db->single();
Expand All @@ -41,10 +45,26 @@ public function findUserByEmail($email){
}
}

public function findUserByUsername($username): bool
{
$this->db->query('SELECT * FROM user WHERE username = :username');
$this->db->bind(':username', $username);

$row = $this->db->single();

// Check row
if ($this->db->rowCount() > 0){
return true;
} else {
return false;
}
}

// Login user
public function login($email, $password){
$this->db->query('SELECT * FROM users WHERE email = :email');
public function login($email, $password, $username){
$this->db->query('SELECT * FROM user WHERE email = :email OR username = :username');
$this->db->bind(':email', $email);
$this->db->bind(':username', $username);

$row = $this->db->single();

Expand Down
77 changes: 77 additions & 0 deletions app/views/driver/vehicles.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php
// include required phpmailer files
require APPROOT . '/libraries/PHPMailer.php';
require APPROOT . '/libraries/SMTP.php';
require APPROOT . '/libraries/Exception.php';

// Define namespace
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

// Create instance of phpmailer
$mail = new PHPMailer();

// Set mailer to use smtp
$mail->isSMTP();

// Define smtp host
$mail->Host = 'smtp.gmail.com';

// Enable smtp authentication
$mail->SMTPAuth = true;

// Set smtp encryption type (ssl/tls)
$mail->SMTPSecure = 'tls';

// Port to connect smtp
$mail->Port = '587';

// Set gmail username
$mail->Username = '[email protected]';

// Set gmail password
$mail->Password = 'Dasun@974';

// Email subject
$mail->Subject = 'Test email from Localhost by Dasun';

// Set sender email
try {
$mail->setFrom('[email protected]');
} catch (Exception $e) {
print_r(e);
}

// Email body
$mail->Body = 'Hello Dasun';

// Add recipient
try {
$mail->addAddress('[email protected]');
} catch (Exception $e) {
print_r(e);
}

// Send email
try {
$mail->Send();
} catch (Exception $e) {
print_r(e);
}

// Close smtp connection
$mail->smtpClose();
?>

<?php require APPROOT.'/views/inc/header.php'; ?>
<!-- TOP NAVIGATION -->
<?php require APPROOT.'/views/inc/components/topnavbar.php'; ?>

<!-- SIDE NAVIGATION -->
<?php require APPROOT.'/views/inc/components/sidenavbar.php'; ?>


<h1>Driver Dashboard</h1>

<?php require APPROOT.'/views/inc/footer.php'; ?>
6 changes: 4 additions & 2 deletions app/views/inc/components/sidenavbar.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
<a class="sidenav-close-btn" onclick="navToggle()">X</a>
<div class="items">
<div class="item selected"><img src="<?php echo URLROOT ?>/images/home.svg" alt="">Dashboard</div>
<div class="item"><img src="<?php echo URLROOT ?>/images/booking.svg" alt="">Bookings</div>
<div class="item"><img src="<?php echo URLROOT ?>/images/booking.svg" alt="">Bookings</a></div>
<div class="item"><img src="<?php echo URLROOT ?>/images/search.svg" alt="">Search Parking</div>
<div class="item"><img src="<?php echo URLROOT ?>/images/history.svg" alt="">Parking History</div>
<div class="item"><img src="<?php echo URLROOT ?>/images/package.svg" alt="">Packages</div>
<div class="item"><img src="<?php echo URLROOT ?>/images/vehicle.svg" alt="">Vehicles</div>
<a href="<?php echo URLROOT ?>/users/login">
<div class="item"><img src="<?php echo URLROOT ?>/images/vehicle.svg" alt="">Vehicles</div>
</a>
<div class="item"><img src="<?php echo URLROOT ?>/images/rating.svg" alt="">Rating</div>
<div class="item"><img src="<?php echo URLROOT ?>/images/profile.svg" alt="">Profile</div>
<div class="logout"><a href="<?php echo URLROOT ?>/users/logout">Logout</a></div>
Expand Down
4 changes: 2 additions & 2 deletions app/views/inc/components/topnavbar.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<div class="topnav">
<div class="container">
<div class="items">
<a class="item logo" onclick="navToggle()"><img style="width: 20px" src="<?php echo URLROOT ?>/images/menu.svg" alt=""></i></a>
<?php if (empty($_SESSION['user_id'])){ ?>
<a class="item" href="<?php echo URLROOT ?>/users/login">Login</a>
<a href="<?php echo URLROOT ?>/users/register">Register</a>
<a class="item" href="<?php echo URLROOT ?>/users/register">Register</a>
<?php }else{ ?>
<a class="item logo" onclick="navToggle()"><img style="width: 20px" src="<?php echo URLROOT ?>/images/menu.svg" alt=""></i></a>
<a class="item" href=""><?php echo $_SESSION['user_name'] ?></a>
<?php } ?>
</div>
Expand Down
3 changes: 3 additions & 0 deletions app/views/security/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
<!-- TOP NAVIGATION -->
<?php require APPROOT.'/views/inc/components/topnavbar.php'; ?>

<!-- SIDE NAVIGATION -->
<?php require APPROOT.'/views/inc/components/sidenavbar.php'; ?>

<h1>Security Dashboard</h1>

<?php require APPROOT.'/views/inc/footer.php'; ?>
2 changes: 1 addition & 1 deletion app/views/users/login.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<form action="<?php echo URLROOT ?>/users/login" method="post">
<!-- Email -->
<div class="form-input-title">Email:</div>
<input type="email" name="email" id="email" required value="<?php echo $data['email'] ?>" />
<input type="text" name="email" id="email" required value="<?php echo $data['email'] ?>" />

<!-- Password -->
<div class="form-input-title">Password:</div>
Expand Down
8 changes: 8 additions & 0 deletions app/views/users/register.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
<?php } ?>

<form action="<?php echo URLROOT ?>/users/register" method="post">
<!-- Username -->
<div class="form-input-title">Username:</div>
<input type="text" name="username" id="username" required value="<?php echo $data['username'] ?>" />

<!-- Name -->
<div class="form-input-title">Name:</div>
<input type="text" name="name" id="name" required value="<?php echo $data['name'] ?>" />
Expand All @@ -19,6 +23,10 @@
<div class="form-input-title">Email:</div>
<input type="email" name="email" id="email" required value="<?php echo $data['email'] ?>" />

<!-- Contact number -->
<div class="form-input-title">Contact number:</div>
<input type="text" name="contact_no" id="contact_no" required value="<?php echo $data['contact_no'] ?>" />

<!-- Password -->
<div class="form-input-title">Password:</div>
<input type="password" name="password" id="password" required />
Expand Down
2 changes: 1 addition & 1 deletion public/css/components/sidenavbar.css
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
padding: 15px 10px 15px 10px;
}

.sidenav .container .items .logout a{
.sidenav .container .items a{
text-decoration: none;
color: black;
}
Expand Down
Loading