Skip to content

Commit

Permalink
Checking and Handling if trying to Reset password of unregistered emails
Browse files Browse the repository at this point in the history
If the email do not exists the user can't proceed in resetting the password
  • Loading branch information
panuozzo77 committed Dec 26, 2023
1 parent 89d36cd commit 5d7a002
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/main/java/controller/SendResetPin.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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");
}
}
}
16 changes: 16 additions & 0 deletions src/main/webapp/JS/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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();
Expand Down

0 comments on commit 5d7a002

Please sign in to comment.