diff --git a/src/main/java/controller/SendResetPin.java b/src/main/java/controller/SendResetPin.java index 3ca0a9d..84f1519 100644 --- a/src/main/java/controller/SendResetPin.java +++ b/src/main/java/controller/SendResetPin.java @@ -1,6 +1,8 @@ package controller; +import model.entity.User; import model.service.login.Authenticator; +import model.service.user.UserData; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; @@ -12,10 +14,15 @@ public class SendResetPin extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException { String email = request.getParameter("email"); - String pin = new Authenticator().resetPassword(email); - // Store the pin and email in the session for future comparison - // Send back a result + UserData checker = new UserData(); + response.setContentType("text/plain"); - response.getWriter().println(pin); + if(checker.checkIfEmailExists(email)){ + String pin = new Authenticator().resetPassword(email); + response.getWriter().println(pin); + } + else { + response.getWriter().println("NA"); + } } } diff --git a/src/main/webapp/CSS/login.css b/src/main/webapp/CSS/login.css index e8e9ed6..e908e28 100644 --- a/src/main/webapp/CSS/login.css +++ b/src/main/webapp/CSS/login.css @@ -26,6 +26,10 @@ a:hover { text-decoration: none; } +.hiddenClass{ + display: none; +} + #loginPage{ width: 100%; display: flex; diff --git a/src/main/webapp/JS/login.js b/src/main/webapp/JS/login.js index 06bf3cf..c2ebb6a 100644 --- a/src/main/webapp/JS/login.js +++ b/src/main/webapp/JS/login.js @@ -1,26 +1,46 @@ -// Extract the element identifiers as variables -let resetEmail = $("#resetEmail"); -let resetStep1 = $("#resetStep1"); -let resetStep2 = $("#resetStep2"); -let resetStep3 = $("#resetStep3"); -let resetPasswordModal = $('#resetPasswordModal'); -let newPassword = $("#newPassword"); -let repeatNewPassword = $("#repeatNewPassword"); -let resetButton = $("#resetPassword"); -let newPasswordError = $(".error"); -let contextPath = window.location.pathname.substring(0, window.location.pathname.indexOf("/",2)); +const resetEmail = $("#resetEmail"); +const resetStep1 = $("#resetStep1"); +const resetStep2 = $("#resetStep2"); +const resetStep3 = $("#resetStep3"); +const resetPasswordModal = $('#resetPasswordModal'); +const newPassword = $("#newPassword"); +const repeatNewPassword = $("#repeatNewPassword"); +const resetButton = $("#resetPassword"); +const newPasswordError = $(".error"); +const showPasswordIcon = $("#showPassword"); +const hidePasswordIcon = $("#hidePassword"); +const validEmailIcon = $("#validEmail"); +const invalidEmailIcon = $("#invalidEmail"); +const contextPath = window.location.pathname.substring(0, window.location.pathname.indexOf("/",2)); let THEPASSWORD; newPassword.on("input", validatePassword); repeatNewPassword.on("input", validatePassword); -$(document).ready(function () { +$(document).ready(function startUp() { + + showPasswordIcon.toggleClass("hiddenClass"); + + $("#showPassword").click((event) => { + event.preventDefault(); + togglePassword(event); + }); + + $("#hidePassword").click((event) => { + event.preventDefault(); + togglePassword(event); + }); + + $("#email").blur(() => { + checkRegexEmail(); + }); $("#forgotPassword").click(handleForgotPassword); - var sessionPin; - $("#sendPin").click(() => { + let sessionPin; + /* + $("#sendPin").click((event) => { event.preventDefault(); $.post(`${contextPath}/login/reset`, {email: resetEmail.val()}, function(response) { sessionPin = response.trim(); // Save the response, which should be your pin @@ -28,8 +48,23 @@ $(document).ready(function () { resetStep2.show(); }); }); + */ + $("#sendPin").click((event) => { + event.preventDefault(); + $.post(`${contextPath}/login/reset`, {email: resetEmail.val()}, function(response) { + response = response.trim(); // Trim the response + if(response == "NA") { + // Here, the servlet will return "NA" if it could not find the email + alert("Email non registrata nel nostro sistema. Verificane la correttezza"); + } else { + sessionPin = response; // Save the response, which should be your pin + resetStep1.hide(); + resetStep2.show(); + } + }); + }); - $("#confirmPin").click(() => { + $("#confirmPin").click((event) => { event.preventDefault(); if ($("#pin").val() === sessionPin) { // if the PIN entered matches the saved PIN, proceed to step 3 @@ -40,7 +75,7 @@ $(document).ready(function () { } }); - $("#resetPassword").click(function() { + $("#resetPassword").click(function(event) { event.preventDefault(); $.post(`${contextPath}/login/resetpassword`, { email: $("#resetEmail").val(), @@ -57,6 +92,44 @@ $(document).ready(function () { }); +function togglePassword(event) { + event.preventDefault(); + + const passwordField = $("#password"); + let passwordType = passwordField.attr('type'); + console.log(passwordType); + + if (passwordType === 'password') { + passwordField.attr("type", "text") + } else { + passwordField.attr("type", "password"); + } + + showPasswordIcon.toggleClass("hiddenClass") + hidePasswordIcon.toggleClass("hiddenClass") +} + +function checkRegexEmail(){ + const regex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/; + const emailField = $("#email"); + + if(regex.test(emailField.val())){ + if(validEmailIcon.hasClass("hiddenClass")){ + validEmailIcon.toggleClass("hiddenClass"); + } + if(!invalidEmailIcon.hasClass("hiddenClass")) { + invalidEmailIcon.toggleClass("hiddenClass"); + } + } else{ + if(!validEmailIcon.hasClass("hiddenClass")){ + validEmailIcon.toggleClass("hiddenClass"); + } + if(invalidEmailIcon.hasClass("hiddenClass")){ + invalidEmailIcon.toggleClass("hiddenClass"); + } + } +} + function handleForgotPassword(event) { event.preventDefault(); resetPasswordModal.modal('show'); @@ -88,7 +161,7 @@ function validatePassword() { showError("Le password non coincidono."); check2 = false; } - if (THEPASSWORD == confirmPassword) { + if (THEPASSWORD === confirmPassword) { showError(""); check2 = true; } diff --git a/src/main/webapp/JSP/login.jsp b/src/main/webapp/JSP/login.jsp index 2988ee1..c181632 100644 --- a/src/main/webapp/JSP/login.jsp +++ b/src/main/webapp/JSP/login.jsp @@ -18,25 +18,25 @@
- - + validEmail + invalidEmail

- emailIcon + passwordIcon
- - + +

- Password Dimenticata? + Password Dimenticata?
@@ -73,9 +73,10 @@ - + + - + \ No newline at end of file