diff --git a/src/API/requests.php b/src/API/requests.php index d3ac587..50d3b3b 100644 --- a/src/API/requests.php +++ b/src/API/requests.php @@ -7,6 +7,7 @@ require_once '../php/db/Search.php'; require_once '../php/db/Login.php'; require_once '../php/db/Signup.php'; require_once '../php/db/Calendrier.php'; +require_once '../php/db/Patient.php'; require_once 'test/database.php'; ini_set('display_errors', 1); error_reporting(E_ALL); @@ -81,6 +82,41 @@ $router->PUT('/api/signup/medecin', ["name", "surname", "mail", "password", "spe SignupMedecin::insertMedecin($pdo, $name, $surname, $mail, $password, $specialite, $phone, $postal); }); +$router->PUT('/api/change/all', ["id", "currentPassword", "newPassword", "currentMail", "newMail", "currentPhone", "newPhone"], function($id, $currentPassword, $newPassword, $currentMail, $newMail, $currentPhone, $newPhone){ + global $pdo; + Patient::updateAll($pdo, $id, $currentPassword, $newPassword, $currentPhone, $newPhone, $currentMail, $newMail); +}); + +$router->PUT('/api/change/mailphone', ["id", "currentMail", "newMail", "currentPhone", "newPhone"], function($id, $currentMail, $newMail, $currentPhone, $newPhone){ + global $pdo; + Patient::updatePhoneAndMail($pdo, $id, $currentPhone, $newPhone, $currentMail, $newMail); +}); + +$router->PUT('/api/change/mailpassword', ["id", "currentMail", "newMail", "currentPassword", "newPassword"], function($id, $currentMail, $newMail, $currentPassword, $newPassword){ + global $pdo; + Patient::updatePasswordAndMail($pdo, $id, $currentPassword, $newPassword, $currentMail, $newMail); +}); + +$router->PUT('/api/change/phonepassword', ["id", "currentPhone", "newPhone", "currentPassword", "newPassword"], function($id, $currentPhone, $newPhone, $currentPassword, $newPassword){ + global $pdo; + Patient::updatePasswordAndPhone($pdo, $id, $currentPassword, $newPassword, $currentPhone, $newPhone); +}); + +$router->PUT('/api/change/password', ["id", "currentPassword", "newPassword"], function($id, $currentPassword, $newPassword){ + global $pdo; + Patient::updatePassword($pdo, $id, $currentPassword, $newPassword); +}); + +$router->PUT('/api/change/phone', ["id", "currentPhone", "newPhone"], function($id, $currentPhone, $newPhone){ + global $pdo; + Patient::updatePhone($pdo, $id, $currentPhone, $newPhone); +}); + +$router->PUT('/api/change/mail', ["id", "currentMail", "newMail"], function($id, $currentMail, $newMail){ + global $pdo; + Patient::updateMail($pdo, $id, $currentMail, $newMail); +}); + $router->PUT('/api/requests', ["test"], function($test){ echo json_encode($test); }); diff --git a/src/css/styles.css b/src/css/styles.css index de23199..8abdb70 100644 --- a/src/css/styles.css +++ b/src/css/styles.css @@ -16,6 +16,19 @@ body{ cursor: pointer; } +.dropdown-menu{ + flex-direction: row; + display: none; +} + +.dropdown-menu.show{ + display: flex; +} + +.dropdown-item { + white-space: nowrap; +} + .login-ui{ height: 78vh; background-color: #EEF2F7; @@ -97,6 +110,7 @@ body{ position: fixed; right: 0; top: 9vh; + z-index: 0; } #cardPos{ diff --git a/src/js/AJAX/home.js b/src/js/AJAX/home.js index 6053068..662d152 100644 --- a/src/js/AJAX/home.js +++ b/src/js/AJAX/home.js @@ -19,6 +19,7 @@ function displayHomeTopBar() {

+ `; attachLoginUserDisplayEventListener(); attachLoginPraticienDisplayEventListener(); @@ -37,13 +38,20 @@ function displayHomeTopBar() {
avatar -
- ${user.name} ${user.surname} + - +
-
`; + + `; attachDisconnectEventListener(); + attachProfileEventListener(); + horizontalDropdown(); } else if (user.type === "medecin") { topbar.innerHTML = ` @@ -55,13 +63,20 @@ function displayHomeTopBar() {
avatar -
- ${user.name} ${user.surname} + - +
-
`; + + `; attachDisconnectEventListener(); + attachProfileEventListener(); + horizontalDropdown(); } else { topbar.innerHTML = ` @@ -127,4 +142,137 @@ function displayHome(text) { } } +function displayProfile() { + let user = TokenDecode(sessionStorage.getItem("token")); + if (user !== null) { + let container = document.getElementById("content"); + container.innerHTML = ""; + container.innerHTML = ` +
+

Profile

+
+ + + + + + + + + + + + + + + + + + + + + + + + + +
+
`; + + document.getElementById("profile-form").addEventListener("submit", function (event) { + event.preventDefault(); + let currentPassword = document.getElementById("password").value; + let newPassword = document.getElementById("new-password").value; + let currentMail = document.getElementById("mail").value; + let newMail = document.getElementById("new-mail").value; + let currentPhone = document.getElementById("phone").value; + let newPhone = document.getElementById("new-phone").value; + changePasswordMailAndPhone(currentPassword, newPassword, currentMail, newMail, currentPhone, newPhone); + }); + } +} + +function changePasswordMailAndPhone(currentPassword, newPassword, currentMail, newMail, currentPhone, newPhone) { + let user = TokenDecode(sessionStorage.getItem("token")); + if (currentPassword && newPassword){ + if (currentPhone && newPhone){ + if(currentMail && newMail) { + let data = "id=" + user.id + "¤tMail=" + currentMail + "&newMail=" + newMail + "¤tPhone=" + currentPhone + "&newPhone=" + newPhone + "¤tPassword=" + currentPassword + "&newPassword=" + newPassword; + ajaxRequest('PUT', "src/API/requests.php/api/change/all", function (data) { + sessionStorage.clear(); + displayHome("Profile mis à jour, veuillez vous reconnecter"); + }, data); + return; + } + let data = "id=" + user.id + "¤tPhone=" + currentPhone + "&newPhone=" + newPhone + "¤tPassword=" + currentPassword + "&newPassword=" + newPassword; + ajaxRequest('PUT', "src/API/requests.php/api/change/phonepassword", function (data) { + sessionStorage.clear(); + displayHome("Profile mis à jour, veuillez vous reconnecter"); + }, data); + return; + } + if (currentMail && newMail){ + let data = "id=" + user.id + "¤tMail=" + currentMail + "&newMail=" + newMail + "¤tPassword=" + currentPassword + "&newPassword=" + newPassword; + ajaxRequest('PUT', "src/API/requests.php/api/change/mailpassword", function (data) { + sessionStorage.clear(); + displayHome("Profile mis à jour, veuillez vous reconnecter") + }, data); + return; + } + let data = "id=" + user.id + "¤tPassword=" + currentPassword + "&newPassword=" + newPassword; + ajaxRequest('PUT', "src/API/requests.php/api/change/password", function (data) { + sessionStorage.clear(); + displayHome("Profile mis à jour, veuillez vous reconnecter") + }, data); + return; + } + if (currentPhone && newPhone){ + if(currentMail && newMail) { + let data = "id=" + user.id + "¤tMail=" + currentMail + "&newMail=" + newMail + "¤tPhone=" + currentPhone + "&newPhone=" + newPhone; + ajaxRequest('PUT', "src/API/requests.php/api/change/mailphone", function (data) { + sessionStorage.clear(); + displayHome("Profile mis à jour, veuillez vous reconnecter") + }, data); + return; + } + let data = "id=" + user.id + "¤tPhone=" + currentPhone + "&newPhone=" + newPhone; + ajaxRequest('PUT', "src/API/requests.php/api/change/phone", function (data) { + sessionStorage.clear(); + displayHome("Profile mis à jour, veuillez vous reconnecter") + }, data); + return; + } + if(currentMail && newMail) { + let data = "id=" + user.id + "¤tMail=" + currentMail + "&newMail=" + newMail; + ajaxRequest('PUT', "src/API/requests.php/api/change/mail", function (data) { + sessionStorage.clear(); + displayHome("Profile mis à jour, veuillez vous reconnecter"); + }, data); + return; + } +} + +function attachProfileEventListener() { + try { + document.getElementById("user-name-profile").addEventListener("click", function () { + displayProfile(); + }); + } + catch (e) { + //Do nothing + //console.error(e); + } +} + +function horizontalDropdown(){ + document.getElementById("user-name-profile").addEventListener("click", function() { + let dropdownMenu = document.querySelector(".dropdown-menu"); + if (dropdownMenu.style.display === "none") { + dropdownMenu.style.display = "flex"; + } else { + dropdownMenu.style.display = "none"; + } + }); +} + export {displayHome, displayHomeTopBar}; \ No newline at end of file diff --git a/src/js/AJAX/lib.js b/src/js/AJAX/lib.js index 251ece4..44abd8e 100644 --- a/src/js/AJAX/lib.js +++ b/src/js/AJAX/lib.js @@ -8,8 +8,8 @@ function TokenDecode(token){ id: decodedToken[1], name: decodedToken[2], surname: decodedToken[3], - mail: decodedToken[4] - + mail: decodedToken[4], + phone : decodedToken[5] }; } export {TokenDecode}; \ No newline at end of file diff --git a/src/js/AJAX/take-rdv.js b/src/js/AJAX/take-rdv.js index 46f8563..092db49 100644 --- a/src/js/AJAX/take-rdv.js +++ b/src/js/AJAX/take-rdv.js @@ -1,5 +1,7 @@ -console.log("take-rdv.js loaded"); import {displayHome} from "./home.js"; +import {TokenDecode} from "./lib.js"; +console.log("take-rdv.js loaded"); + function displayRDVForDate(data) { let container = document.getElementById("rdv-list"); @@ -104,7 +106,7 @@ function displayCalendar(data) { document.getElementById("date-selection-form").addEventListener("submit", function(event) { event.preventDefault(); let date = document.getElementById("date").value; - let id = data[0].m_id; // Assuming 'data' is accessible in this scope and contains the doctor's details + let id = data[0].m_id; ajaxRequest('GET', "src/API/requests.php/api/rdv-date?date=" + date + "&id=" + id, function (data) { displayRDVForDate(data); }); @@ -133,15 +135,22 @@ function attachPrendreRDVEventListener() { displayHome("Vous devez être connecté pour prendre un rendez-vous"); } else{ - let RDVid = event.target.id.split("-")[2]; - let data = "rdv_id=" + RDVid + "&patient_id=" + 1; - ajaxRequest('PUT', "src/API/requests.php/api/rdv", function (returnData){ - if (returnData.success) { - displayHome("Rendez-vous pris avec succès"); - } else { - alert("Erreur lors de la prise de rendez-vous"); - } - }, data); + let decodedToken = TokenDecode(sessionStorage.getItem("token")); + if(decodedToken.type !== "patient") { + displayHome("Vous devez être un patient pour prendre un rendez-vous"); + } + else { + let RDVid = event.target.id.split("-")[2]; + const id = decodedToken.id; + let data = "rdv_id=" + RDVid + "&patient_id=" + id; + ajaxRequest('PUT', "src/API/requests.php/api/rdv", function (returnData){ + if (returnData.success) { + displayHome("Rendez-vous pris avec succès"); + } else { + alert("Erreur lors de la prise de rendez-vous"); + } + }, data); + } } }); }); diff --git a/src/js/AJAX/utils.js b/src/js/AJAX/utils.js index 3425758..9f96729 100644 --- a/src/js/AJAX/utils.js +++ b/src/js/AJAX/utils.js @@ -18,7 +18,6 @@ function ajaxRequest(type, url, callback, data = null) { case 200: case 201: - //console.log(xhr.responseText); callback(JSON.parse(xhr.responseText)); break; default: diff --git a/src/js/signup.js b/src/js/signup.js deleted file mode 100644 index b9c8b91..0000000 --- a/src/js/signup.js +++ /dev/null @@ -1,43 +0,0 @@ -function checkMailMatch(mailContainer, mailConfirmContainer, signupButton) { - if (mailContainer.value !== mailConfirmContainer.value) { - signupButton.classList.add("disabled"); - document.getElementById("mail-error").innerHTML = "Emails don't match"; - } else { - signupButton.classList.remove("disabled"); - document.getElementById("mail-error").innerHTML = ""; - } -} - -function checkPasswordMatch(passwordContainer, passwordConfirmContainer, signupButton) { - if (passwordContainer.value !== passwordConfirmContainer.value) { - signupButton.classList.add("disabled"); - document.getElementById("password-error").innerHTML = "Passwords don't match"; - } else { - signupButton.classList.remove("disabled"); - document.getElementById("password-error").innerHTML = ""; - } -} - -document.addEventListener("DOMContentLoaded", function() { - const mailContainer = document.getElementById("mail"); - const mailConfirmContainer = document.getElementById("mailConfirmation"); - const passwordContainer = document.getElementById("password"); - const passwordConfirmContainer = document.getElementById("passwordConfirmation"); - const signupButton = document.getElementById("signupButton"); - - mailContainer.addEventListener("keyup", function() { - checkMailMatch(mailContainer, mailConfirmContainer, signupButton); - }); - - mailConfirmContainer.addEventListener("keyup", function() { - checkMailMatch(mailContainer, mailConfirmContainer, signupButton); - }); - - passwordContainer.addEventListener("keyup", function() { - checkPasswordMatch(passwordContainer, passwordConfirmContainer, signupButton); - }); - - passwordConfirmContainer.addEventListener("keyup", function() { - checkPasswordMatch(passwordContainer, passwordConfirmContainer, signupButton); - }); -}); \ No newline at end of file diff --git a/src/php/db/Login.php b/src/php/db/Login.php index 80a3b9f..bccf12b 100644 --- a/src/php/db/Login.php +++ b/src/php/db/Login.php @@ -102,13 +102,13 @@ class LoginPatient { public static function Login($pdo,$mail,$password) : void { if(LoginPatient::checkMail($pdo,$mail)){ - $query = $pdo->prepare("SELECT p_id,p_password,p_name,p_surname,p_mail FROM patient where p_mail = :mail"); + $query = $pdo->prepare("SELECT p_id,p_password,p_name,p_surname,p_mail,p_phone FROM patient where p_mail = :mail"); $query->bindParam(':mail', $mail); $query->execute(); $result = $query->fetch(PDO::FETCH_ASSOC); if(password_verify($password,$result['p_password'])){ - Response::HTTP200(['message' => 'Login success', 'token' => base64_encode(bin2hex("patient:".strval($result['p_id']).":".$result['p_name'].":".$result['p_surname'].":".$result['p_mail']))]); + Response::HTTP200(['message' => 'Login success', 'token' => base64_encode(bin2hex("patient:".strval($result['p_id']).":".$result['p_name'].":".$result['p_surname'].":".$result['p_mail'].":".$result['p_phone']))]); } else { Response::HTTP401(['message' => 'Login failed', 'token' => 0]); } diff --git a/src/php/db/Patient.php b/src/php/db/Patient.php index 925a39a..45d44ba 100644 --- a/src/php/db/Patient.php +++ b/src/php/db/Patient.php @@ -26,6 +26,199 @@ class Patient { } + public static function updatePassword($pdo, $id, $currentpassword, $newpassword): void + { + $query = $pdo->prepare("SELECT p_password FROM patient WHERE p_id = :id"); + $query->bindParam(':id', $id); + $query->execute(); + $result = $query->fetch(PDO::FETCH_ASSOC); + if (password_verify($currentpassword, $result['p_password'])) { + $newpassword = password_hash($newpassword, PASSWORD_ARGON2ID); + $query = $pdo->prepare("UPDATE patient SET p_password = :password WHERE p_id = :id"); + $query->bindParam(':password', $newpassword); + $query->bindParam(':id', $id); + $query->execute(); + } + // Check if the current password is correct + $query = $pdo->prepare("SELECT p_password FROM patient WHERE p_id = :id"); + $query->bindParam(':id', $id); + $query->execute(); + $result = $query->fetch(PDO::FETCH_ASSOC); + if (password_verify($newpassword, $result['p_password'])) { + Response::HTTP201(['message' => 'Password updated']); + } + else { + Response::HTTP400(['message' => 'Error updating password']); + } + } + public static function updatePhone($pdo, $id, $currentphone, $newphone): void + { + $query = $pdo->prepare("SELECT p_phone FROM patient WHERE p_id = :id"); + $query->bindParam(':id', $id); + $query->execute(); + $result = $query->fetch(PDO::FETCH_ASSOC); + if ($currentphone == $result['p_phone']) { + $query = $pdo->prepare("UPDATE patient SET p_phone = :phone WHERE p_id = :id"); + $query->bindParam(':phone', $newphone); + $query->bindParam(':id', $id); + $query->execute(); + } + // Check if the current phone is correct + $query = $pdo->prepare("SELECT p_phone FROM patient WHERE p_id = :id"); + $query->bindParam(':id', $id); + $query->execute(); + $result = $query->fetch(PDO::FETCH_ASSOC); + if ($newphone == $result['p_phone']) { + Response::HTTP201(['message' => 'Phone updated']); + } + else { + Response::HTTP400(['message' => 'Error updating phone']); + } + } + + public static function updateMail($pdo, $id, $currentmail, $newmail): void + { + $query = $pdo->prepare("SELECT p_mail FROM patient WHERE p_id = :id"); + $query->bindParam(':id', $id); + $query->execute(); + $result = $query->fetch(PDO::FETCH_ASSOC); + if ($currentmail == $result['p_mail']) { + $query = $pdo->prepare("UPDATE patient SET p_mail = :mail WHERE p_id = :id"); + $query->bindParam(':mail', $newmail); + $query->bindParam(':id', $id); + $query->execute(); + } + // Check if the current mail is correct + $query = $pdo->prepare("SELECT p_mail FROM patient WHERE p_id = :id"); + $query->bindParam(':id', $id); + $query->execute(); + $result = $query->fetch(PDO::FETCH_ASSOC); + if (password_verify($newmail, $result['p_mail'])) { + Response::HTTP201(['message' => 'Mail updated']); + } + else { + Response::HTTP400(['message' => 'Error updating mail']); + } + } + + public static function updatePasswordAndPhone($pdo, $id, $currentpassword, $newpassword, $currentphone, $newphone): void + { + $query = $pdo->prepare("SELECT p_password, p_phone FROM patient WHERE p_id = :id"); + $query->bindParam(':id', $id); + $query->execute(); + $result = $query->fetch(PDO::FETCH_ASSOC); + if (password_verify($currentpassword, $result['p_password']) && $currentphone == $result['p_phone']) { + $newpassword = password_hash($newpassword, PASSWORD_ARGON2ID); + $query = $pdo->prepare("UPDATE patient SET p_password = :password, p_phone = :phone WHERE p_id = :id"); + $query->bindParam(':password', $newpassword); + $query->bindParam(':phone', $newphone); + $query->bindParam(':id', $id); + $query->execute(); + } + // Check if the current password and phone are correct + $query = $pdo->prepare("SELECT p_password, p_phone FROM patient WHERE p_id = :id"); + $query->bindParam(':id', $id); + $query->execute(); + $result = $query->fetch(PDO::FETCH_ASSOC); + if (password_verify($newpassword, $result['p_password']) && $newphone == $result['p_phone']) { + Response::HTTP201(['message' => 'Password and phone updated']); + } + else { + Response::HTTP400(['message' => 'Error updating password and phone']); + } + } + + public static function updatePasswordAndMail($pdo, $id, $currentpassword, $newpassword, $currentmail, $newmail): void + { + $query = $pdo->prepare("SELECT p_password, p_mail FROM patient WHERE p_id = :id"); + $query->bindParam(':id', $id); + $query->execute(); + $result = $query->fetch(PDO::FETCH_ASSOC); + if (password_verify($currentpassword, $result['p_password']) && $currentmail == $result['p_mail']) { + $newpassword = password_hash($newpassword, PASSWORD_ARGON2ID); + $query = $pdo->prepare("UPDATE patient SET p_password = :password, p_mail = :mail WHERE p_id = :id"); + $query->bindParam(':password', $newpassword); + $query->bindParam(':mail', $newmail); + $query->bindParam(':id', $id); + $query->execute(); + } + // Check if the current password and mail are correct + $query = $pdo->prepare("SELECT p_password, p_mail FROM patient WHERE p_id = :id"); + $query->bindParam(':id', $id); + $query->execute(); + $result = $query->fetch(PDO::FETCH_ASSOC); + if (password_verify($newpassword, $result['p_password']) && $newmail == $result['p_mail']) { + Response::HTTP201(['message' => 'Password and mail updated']); + } + else { + Response::HTTP400(['message' => 'Error updating password and mail']); + } + } + + public static function updatePhoneAndMail($pdo, $id, $currentphone, $newphone, $currentmail, $newmail): void + { + $query = $pdo->prepare("SELECT p_phone, p_mail FROM patient WHERE p_id = :id"); + $query->bindParam(':id', $id); + $query->execute(); + $result = $query->fetch(PDO::FETCH_ASSOC); + if ($currentphone == $result['p_phone'] && $currentmail == $result['p_mail']) { + $query = $pdo->prepare("UPDATE patient SET p_phone = :phone, p_mail = :mail WHERE p_id = :id"); + $query->bindParam(':phone', $newphone); + $query->bindParam(':mail', $newmail); + $query->bindParam(':id', $id); + $query->execute(); + } + // Check if the current phone and mail are correct + $query = $pdo->prepare("SELECT p_phone, p_mail FROM patient WHERE p_id = :id"); + $query->bindParam(':id', $id); + $query->execute(); + $result = $query->fetch(PDO::FETCH_ASSOC); + if ($newphone == $result['p_phone'] && $newmail == $result['p_mail']) { + Response::HTTP201(['message' => 'Phone and mail updated']); + } + else { + Response::HTTP400(['message' => 'Error updating phone and mail']); + } + } + + public static function updateAll($pdo, $id, $currentpassword, $newpassword, $currentphone, $newphone, $currentmail, $newmail): void + { + $query = $pdo->prepare("SELECT p_password, p_phone, p_mail FROM patient WHERE p_id = :id"); + $query->bindParam(':id', $id); + $query->execute(); + $result = $query->fetch(PDO::FETCH_ASSOC); + $newpassword = password_hash($newpassword, PASSWORD_ARGON2ID); + if(!password_verify($currentpassword, $result['p_password'])){ + Response::HTTP401(['message' => 'Current password is incorrect']); + } + if (password_verify($currentpassword, $result['p_password']) && $currentphone == $result['p_phone'] && $currentmail == $result['p_mail']) { + $query = $pdo->prepare("UPDATE patient SET p_password = :password, p_phone = :phone, p_mail = :mail WHERE p_id = :id"); + $query->bindParam(':password', $newpassword); + $query->bindParam(':phone', $newphone); + $query->bindParam(':mail', $newmail); + $query->bindParam(':id', $id); + $query->execute(); + } + // Check if the current password, phone and mail are correct + $query = $pdo->prepare("SELECT p_password, p_phone, p_mail FROM patient WHERE p_id = :id"); + $query->bindParam(':id', $id); + $query->execute(); + $result = $query->fetch(PDO::FETCH_ASSOC); + if (password_verify($newpassword, $result['p_password']) && $newphone == $result['p_phone'] && $newmail == $result['p_mail']) { + Response::HTTP201(['message' => 'All updated']); + } + else { + if($newphone != $result['p_phone']){ + Response::HTTP400(['message' => 'Error updating phone']); + } + if($newmail != $result['p_mail']){ + Response::HTTP400(['message' => 'Error updating mail']); + } + if(!password_verify($newpassword, $result['p_password'])){ + Response::HTTP400(['message' => 'Error updating password']); + } + } + } } ?> \ No newline at end of file