-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResendOTPServlet.java
More file actions
56 lines (45 loc) · 1.89 KB
/
Copy pathResendOTPServlet.java
File metadata and controls
56 lines (45 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package com.securefileshare.servlets;
import com.securefileshare.models.User;
import com.securefileshare.services.OTPService;
import com.securefileshare.services.EmailService;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
public class ResendOTPServlet extends HttpServlet {
private OTPService otpService;
private EmailService emailService;
@Override
public void init() throws ServletException {
otpService = new OTPService();
emailService = new EmailService();
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession(false);
if (session == null) {
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
return;
}
User pendingUser = (User) session.getAttribute("pendingUser");
String purpose = (String) session.getAttribute("otpPurpose");
if (pendingUser == null || purpose == null) {
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
return;
}
String newOTP = otpService.generateOTP(session, pendingUser.getEmail(), purpose);
try {
emailService.sendOTPEmail(
pendingUser.getEmail(),
pendingUser.getUsername(),
newOTP,
purpose
);
System.out.println("DEBUG: New OTP " + newOTP + " sent to " + pendingUser.getEmail());
} catch (Exception e) {
System.err.println("DEBUG: Email sending failed, but OTP is available on screen: " + newOTP);
}
response.setStatus(HttpServletResponse.SC_OK);
}
}