From 5d7a002be636c2a38fd42324933f6b3658706f49 Mon Sep 17 00:00:00 2001 From: panuozzo77 Date: Tue, 26 Dec 2023 17:31:57 +0100 Subject: [PATCH] Checking and Handling if trying to Reset password of unregistered emails If the email do not exists the user can't proceed in resetting the password --- src/main/java/controller/SendResetPin.java | 15 +++++++++++---- src/main/webapp/JS/login.js | 16 ++++++++++++++++ 2 files changed, 27 insertions(+), 4 deletions(-) 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/JS/login.js b/src/main/webapp/JS/login.js index 0554178..c2ebb6a 100644 --- a/src/main/webapp/JS/login.js +++ b/src/main/webapp/JS/login.js @@ -39,6 +39,7 @@ $(document).ready(function startUp() { $("#forgotPassword").click(handleForgotPassword); let sessionPin; + /* $("#sendPin").click((event) => { event.preventDefault(); $.post(`${contextPath}/login/reset`, {email: resetEmail.val()}, function(response) { @@ -47,6 +48,21 @@ $(document).ready(function startUp() { 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((event) => { event.preventDefault();