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();