Skip to content

Commit

Permalink
login and session controls, home screen schanges
Browse files Browse the repository at this point in the history
  • Loading branch information
kutluhanazafli committed Jun 24, 2024
1 parent fdd5c79 commit 54de104
Show file tree
Hide file tree
Showing 10 changed files with 162 additions and 15 deletions.
27 changes: 27 additions & 0 deletions Controller/login.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,37 @@
<?php

if(get_session('login') && get_session('login') == true) {
redirect('home');
}

if (route(0) == 'login') {
if (isset($_POST['submit'])) {

$_SESSION['post'] = $_POST;

$email = post('email');
$password = post('password');


$return = model('auth/login', [
'email' => $email,
'password' => $password
], 'login');

if($return['success'] == true) {
add_session('error', [
'type' => $return['type'] ?? '',
'message' => $return['message'] ?? ''
]);
if (isset($return['redirect'])) {
redirect($return['redirect']);
}
} else {
add_session('error', [
'type' => $return['type'] ?? '',
'message' => $return['message'] ?? ''
]);
}
}

view('auth/login');
Expand Down
4 changes: 4 additions & 0 deletions Controller/logout.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php

session_destroy();
redirect('login');
54 changes: 54 additions & 0 deletions Model/auth/login.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

if ($process == 'login') {

if (!$data['email']) {
return [
'success' => false,
'message' => 'E-mail address is required',
'type' => 'danger'
];
}
if (!$data['password']) {
return [
'success' => false,
'message' => 'Password is required',
'type' => 'danger'
];
}

$q = $db -> prepare("SELECT *, CONCAT(user_name, ' ', user_surname) as user_fullname FROM users WHERE user_email = ? AND user_password = ?");
$q -> execute([
$data['email'],
hash('SHA512', $data['password'])
]);

if ($q -> rowCount()) {
$user = $q -> fetch(PDO::FETCH_ASSOC);
add_session('user_id', $user['user_id']);
add_session('user_name', $user['user_name']);
add_session('user_surname', $user['user_surname']);
add_session('user_fullname', $user['user_fullname']);
add_session('user_email', $user['user_email']);
add_session('login', true);

return [
'data' => $user,
'success' => true,
'message' => 'Login successful. Redirecting...',
'type' => 'success',
'redirect' => 'home'
];

} else {

return [
'success' => false,
'message' => 'Login failed. Please check your credentials.',
'type' => 'danger'
];

}

}

Empty file removed README.MD
Empty file.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# ToDo Project

This is a simple ToDo project built with PHP using the Model-View-Controller (MVC) architectural pattern.

## What I Did

- Routing and Functionality of MVC Structure
- Multilingual Support
- Database Preparation
- Login Operations
- Profile and Password Update Operations
- Todo Categories (Each session creates its own category)
- Adding
- Deleting
- Updating
- Listing
- Todos (Each session creates its own list)
- Adding
- Updating
- Deleting
- Listing
- In Progress - Completed
- FullCalendarAPI Integration
- Timeline Display
- Statistics
8 changes: 5 additions & 3 deletions View/auth/login.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,20 @@
<div class="card">
<div class="card-body login-card-body">
<p class="login-box-msg"> <?= lang('login') ?> </p>

<?php
echo get_session('error') ? '<div class="alert alert-' . $_SESSION['error']['type'] . '">' . $_SESSION['error']['message'] . '</div>' : null;
?>
<form action="<?= URL . 'login' ?>" method="post">
<div class="input-group mb-3">
<input type="email" class="form-control" name="email" placeholder=" <?= lang('email') ?> ">
<input type="email" class="form-control" value="<?= $_SESSION['post']['email'] ?? '' ?>" name="email" placeholder=" <?= lang('email') ?> ">
<div class="input-group-append">
<div class="input-group-text">
<span class="fas fa-envelope"></span>
</div>
</div>
</div>
<div class="input-group mb-3">
<input type="password" class="form-control" name="password" placeholder=" <?= lang('password') ?> ">
<input type="password" class="form-control" value="<?= $_SESSION['post']['password'] ?? '' ?>" name="password" placeholder=" <?= lang('password') ?> ">
<div class="input-group-append">
<div class="input-group-text">
<span class="fas fa-lock"></span>
Expand Down
13 changes: 7 additions & 6 deletions View/home/home.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<a class="nav-link" data-widget="pushmenu" href="#" role="button"><i class="fas fa-bars"></i></a>
</li>
<li class="nav-item d-none d-sm-inline-block">
<a href="index3.html" class="nav-link">Home</a>
<a href="<?= URL . 'logout'; ?>" class="nav-link">Logout</a>
</li>
<li class="nav-item d-none d-sm-inline-block">
<a href="#" class="nav-link">Contact</a>
Expand Down Expand Up @@ -144,19 +144,20 @@
<aside class="main-sidebar sidebar-dark-primary elevation-4">
<!-- Brand Logo -->
<a href="index3.html" class="brand-link">
<img src="dist/img/AdminLTELogo.png" alt="AdminLTE Logo" class="brand-image img-circle elevation-3" style="opacity: .8">
<span class="brand-text font-weight-light">AdminLTE 3</span>
<!-- <img src="dist/img/AdminLTELogo.png" alt="AdminLTE Logo" class="brand-image img-circle elevation-3" style="opacity: .8"> -->
<i class="fa fa-check-square"></i>
<span class="brand-text font-weight-light">Todo</span>
</a>

<!-- Sidebar -->
<div class="sidebar">
<!-- Sidebar user panel (optional) -->
<div class="user-panel mt-3 pb-3 mb-3 d-flex">
<div class="image">
<img src="dist/img/user2-160x160.jpg" class="img-circle elevation-2" alt="User Image">
<div class="image bg-warning d-flex allign-item-center justify-content-center p-2">
<i class="fa fa-user"></i>
</div>
<div class="info">
<a href="#" class="d-block">Alexander Pierce</a>
<a href="#" class="d-block"><?= get_session('user_fullname') ?></a>
</div>
</div>

Expand Down
21 changes: 15 additions & 6 deletions config/config.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
<?php

// Set BASEDIR as the root directory of the project
$basedir = dirname(__DIR__) . DIRECTORY_SEPARATOR;
$basedir = dirname(__DIR__);
define('BASEDIR', $basedir);


// Set URL dynamically by checking folder name of the project
$url = 'http://localhost/' . (basename(BASEDIR));
$url = 'http://localhost/' . (basename(BASEDIR) . '/');
define('URL', $url);
echo BASEDIR . '<br>';
echo URL;

const DEV_MODE = true;
const DEV_MODE = true;

// Database connection
$host = 'bzoighyemc4n1alzqpup-mysql.services.clever-cloud.com';
$dbname = 'bzoighyemc4n1alzqpup';
$username = 'uzwnv0ianiryk6xp';
$password = 'ZuIKwJqDz5gyR9C2uMvF';

try {
$db = new PDO("mysql:host=".$host.";dbname=".$dbname.";charset=utf8", $username, $password);
} catch (PDOException $e) {
die('Connection failed: ' . $e->getMessage());
}
18 changes: 18 additions & 0 deletions helpers/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@ function view($viewName, $pageData = []){
return false;
}

function model($modelName, $pageData = [], $data_process = null){
global $db;
if ($data_process != null){
$process = $data_process;
}
$data = $pageData;

if(file_exists(BASEDIR . '/Model/' . $modelName . '.php')) {
$return = require BASEDIR . '/Model/' . $modelName . '.php';
return $return;
} else
return false;
}

function assets($assetName){
if(file_exists(BASEDIR . '/public/' . $assetName))
return URL . '/public/' . $assetName;
Expand Down Expand Up @@ -66,4 +80,8 @@ function get_cookie($index) {
} else {
return false;
}
}

function redirect($url) {
header('Location: ' . URL . $url);
}
7 changes: 7 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,10 @@
}


if (isset($_SESSION['error'])) {
unset($_SESSION['error']);
}

if (isset($_SESSION['post'])) {
unset($_SESSION['post']);
}

0 comments on commit 54de104

Please sign in to comment.