-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
222 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
function is_input_empty(string $email, string $password) | ||
{ | ||
if (empty($email) || empty($password)) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
|
||
function is_email_invalid(string $email) | ||
{ | ||
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
|
||
function is_email_wrong(bool|array $result) | ||
{ | ||
if (!$result) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
function is_password_wrong(string $pass, string $hashed_pass) | ||
{ | ||
if (!password_verify($pass, $hashed_pass)) { | ||
return true; | ||
} | ||
return false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
if ($_SERVER['REQUEST_METHOD'] === 'POST') { | ||
$email = $_POST['email']; | ||
$pass = $_POST['password']; | ||
|
||
try { | ||
require_once "./db_handler.inc.php"; | ||
require_once "../models/login.inc.php"; | ||
require_once "../controllers/login.inc.php"; | ||
|
||
// Error handlers | ||
|
||
$errors = []; | ||
|
||
// Check for empty inputs | ||
if (is_input_empty($email, $pass)) { | ||
// make sure to use local variables here | ||
$errors["empty_input"] = "Please fill in all fields"; | ||
} | ||
|
||
// Check if email is invalid | ||
if (is_email_invalid($email)) { | ||
$errors["invalid_email"] = "Please enter a valid email address"; | ||
} | ||
|
||
// fetch user from database | ||
$result = get_user($pdo, $email); | ||
|
||
// Check if email is wrong | ||
if (is_email_wrong($result)) { | ||
$errors["login_incorrect"] = "Email or password is incorrect"; | ||
} | ||
// check if email is right and password is wrong | ||
if (!is_email_wrong($result) && is_password_wrong($pass, $result["password"])) { | ||
$errors["login_incorrect"] = "Email or password is incorrect"; | ||
} | ||
|
||
require_once "./config_session.inc.php"; | ||
|
||
if ($errors) { | ||
$_SESSION["errors_login"] = $errors; | ||
header('Location: ../pages/login.php'); | ||
die(); | ||
} | ||
|
||
// generate new session id and append user id to it | ||
$new_session_id = session_create_id(); | ||
$session_id = $new_session_id . "_" . $result["id"]; | ||
session_id($session_id); // set the new session id | ||
|
||
// set session variables | ||
$_SESSION["user_id"] = $result["id"]; | ||
$_SESSION["user_username"] = htmlspecialchars($result["username"]); | ||
$_SESSION["user_fullname"] = htmlspecialchars($result["fullname"]); | ||
$_SESSION["last_regeneration"] = time(); | ||
|
||
// redirect to home page | ||
header('Location: ../index.php?login=success'); | ||
|
||
// close connection | ||
$pdo = null; | ||
$stmt = null; | ||
|
||
die(); | ||
} catch (PDOException $e) { | ||
die("Query failed: " . $e->getMessage()); | ||
} | ||
} else { | ||
header('Location: ../index.php'); | ||
die(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,13 @@ | ||
const closeBtn = document.querySelector('.modal__close'); | ||
closeBtn.addEventListener('click', () => { | ||
let parent = document.querySelector('.modal'); | ||
parent.remove(); | ||
}); | ||
let modal = document.querySelector('.modal'); | ||
|
||
if (modal) { | ||
let closeBtn = document.querySelector('.modal__close'); | ||
|
||
// add event listener only if closeBtn exists | ||
if (closeBtn) { | ||
closeBtn.addEventListener('click', function () { | ||
let parent = document.querySelector('.modal'); | ||
parent.remove(); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
function get_user(object $pdo, string $email) | ||
{ | ||
$query = "SELECT * FROM users WHERE email = :email"; // :email is a named placeholder | ||
$stmt = $pdo->prepare($query); // prepare the query | ||
$stmt->bindParam(":email", $email, PDO::PARAM_STR); // bind the $email variable to the :email placeholder | ||
$stmt->execute(); // execute the query | ||
|
||
$result = $stmt->fetch(PDO::FETCH_ASSOC); //fetch the result from the query | ||
return $result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
function check_and_print_login_errors() | ||
{ | ||
|
||
if (isset($_SESSION["errors_login"])) { | ||
$errors = $_SESSION["errors_login"]; | ||
if (count($errors) > 0) { | ||
echo "<section class='modal modal--error'>"; | ||
echo "<h1 class='modal__title'>Errors occurred while logging in: </h1>"; | ||
echo "<span class='modal__close modal__close--error'>X</span>"; | ||
foreach ($errors as $error) { | ||
echo "<p class='modal__item'>$error</p>"; | ||
} | ||
echo "</section>"; | ||
unset($_SESSION["errors_login"]); | ||
} | ||
} | ||
} |