Skip to content

Commit

Permalink
add feature - OTP
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyenkhanhquy committed Dec 6, 2023
1 parent 89bc7d3 commit 8dfe073
Show file tree
Hide file tree
Showing 11 changed files with 411 additions and 57 deletions.
41 changes: 6 additions & 35 deletions src/main/java/com/jacobin/controllers/RegisterController.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
package com.jacobin.controllers;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import javax.mail.MessagingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.jacobin.dao.CartDB;
import com.jacobin.dao.RoleDB;
import com.jacobin.dao.UserDB;
import com.jacobin.models.Cart;
import com.jacobin.models.Role;
import com.jacobin.models.User;
import com.jacobin.utils.PasswordEncryptorUtil;

import com.jacobin.utils.MailUtilGmail;

@WebServlet(urlPatterns = { "/register" })
public class RegisterController extends HttpServlet {

Expand Down Expand Up @@ -75,41 +70,17 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp)
} else if (!user.getPassword().equals(passwordAgain)) {
message = "Mật khẩu không khớp.<br>" + "Vui lòng nhập lại.";
} else {
message = "Đăng ký thành công!";
url = "/WEB-INF/views/customer/successView.jsp";
password = PasswordEncryptorUtil.toSHA1(password);
user.setPassword(password);
UserDB.insert(user);

Cart cart = new Cart();
cart.setUser(user);
CartDB.insert(cart);

// Gửi email đến email của user
String to = email;
String from = "shop.javamail@gmail.com";
String subject = "Chào mừng đến với Jacobin Store";
String body = "Chào " + firstName + ",\n\n"
+ "Chúng tôi rất vui mừng thông báo rằng bạn đã đăng ký thành công tài khoản mới tại Jacobin Store!\n\n"
+ "Chào mừng bạn đến với cửa hàng của chúng tôi và cảm ơn bạn đã chọn chúng tôi để trải nghiệm mua sắm trực tuyến.\n\n"
+ "Hãy khám phá thế giới mua sắm tuyệt vời tại Jacobin Store ngay bây giờ.\n\n"
+ "Chúc bạn có những trải nghiệm mua sắm thú vị và hài lòng!\n\n"
+ "Trân trọng, Jacobin Store.";
boolean isBodyHTML = false;

try {
MailUtilGmail.sendMail(to, from, subject, body, isBodyHTML);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
HttpSession session = req.getSession();
session.setAttribute("user", user);
message = "";
url = "/WEB-INF/views/customer/verifyOTPView.jsp";
}

req.setAttribute("user", user);
req.setAttribute("message", message);
getServletContext().getRequestDispatcher(url).forward(req, resp);
req.getRequestDispatcher(url).forward(req, resp);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package com.jacobin.controllers.customer;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Random;

import javax.mail.MessagingException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.jacobin.dao.UserDB;
import com.jacobin.models.User;
import com.jacobin.utils.MailUtilGmail;
import com.jacobin.utils.PasswordEncryptorUtil;

@WebServlet(urlPatterns = {"/forgot-password"})
public class ForgotPasswordController extends HttpServlet {

private static final long serialVersionUID = 1L;

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {

String url = "/WEB-INF/views/customer/forgotPasswordView.jsp";

HttpSession session = req.getSession();
String email = null;
String message = null;
String action = req.getParameter("action");
if (action == null) {
action = "";
} else {
email = req.getParameter("email");
if (action.equals("send")) {
User user = UserDB.selectUserByEmail(email);
if (user == null) {
message = "Email không tồn tại trên hệ thống!";
}
else {
message = "Đã gửi mã OTP tới email của bạn!";

Random random = new Random();
int otp = random.nextInt(900000) + 100000;
String otpString = String.valueOf(otp);
session.setAttribute("otpSend", otpString);

// Gửi email đến email của user
String to = user.getEmail();
String from = "shop.javamail@gmail.com";
String subject = "Xác minh email";
String body = "Chào " + user.getFirstName() + ",\n\n"
+ "Mã OTP của bạn là: " + otpString;
boolean isBodyHTML = false;

try {
MailUtilGmail.sendMail(to, from, subject, body, isBodyHTML);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} else if (action.equals("confirm")) {
String otp = req.getParameter("otp");

String otpSend = (String) session.getAttribute("otpSend");

if (otp.equals(otpSend)) {
url = "/WEB-INF/views/customer/newPasswordView.jsp";
}
else {
message = "Mã OTP bạn nhập không đúng!";
}
} else if (action.equals("newpass")){
url = "/WEB-INF/views/customer/newPasswordView.jsp";

String newPassword = req.getParameter("newPassword");
String newPasswordAgain = req.getParameter("newPasswordAgain");

email = req.getParameter("email");
User user = UserDB.selectUserByEmail(email);

if (!newPassword.equals(newPasswordAgain)) {
message = "Mật khẩu nhập lại không khớp!";
} else {
message = "Đổi mật khẩu thành công!";
url = "/WEB-INF/views/customer/successView.jsp";
newPassword = PasswordEncryptorUtil.toSHA1(newPassword);
user.setPassword(newPassword);
UserDB.update(user);

session.removeAttribute("otpSend");
}
} else {
message = "";
}
}
req.setAttribute("email", email);
req.setAttribute("message", message);
req.getRequestDispatcher(url).forward(req, resp);
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {

doGet(req, resp);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package com.jacobin.controllers.customer;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Random;

import javax.mail.MessagingException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.jacobin.dao.CartDB;
import com.jacobin.dao.UserDB;
import com.jacobin.models.Cart;
import com.jacobin.models.User;
import com.jacobin.utils.MailUtilGmail;

@WebServlet(urlPatterns = { "/verify-otp" })
public class VerifyOTPController extends HttpServlet {

private static final long serialVersionUID = 1L;

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {

resp.sendRedirect("home");
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {

String url = "/WEB-INF/views/customer/verifyOTPView.jsp";

String action = req.getParameter("action");
if (action == null) {
action = "";
}

HttpSession session = req.getSession();
User user = (User) session.getAttribute("user");

String message;
if (action.equals("send")) {
message = "Đã gửi mã OTP tới email của bạn!";

Random random = new Random();
int otp = random.nextInt(900000) + 100000;
String otpString = String.valueOf(otp);
session.setAttribute("otpSend", otpString);

// Gửi email đến email của user
String to = user.getEmail();
String from = "shop.javamail@gmail.com";
String subject = "Xác minh email";
String body = "Chào " + user.getFirstName() + ",\n\n"
+ "Mã OTP của bạn là: " + otpString;
boolean isBodyHTML = false;

try {
MailUtilGmail.sendMail(to, from, subject, body, isBodyHTML);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else if (action.equals("confirm")) {
String otp = req.getParameter("otp");

String otpSend = (String) session.getAttribute("otpSend");
if (otp.equals(otpSend)) {
message = "Đăng ký thành công!";
url = "/WEB-INF/views/customer/successView.jsp";
UserDB.insert(user);

Cart cart = new Cart();
cart.setUser(user);
CartDB.insert(cart);

// Gửi email đến email của user
String to = user.getEmail();
String from = "shop.javamail@gmail.com";
String subject = "Chào mừng đến với Jacobin Store";
String body = "Chào " + user.getFirstName() + ",\n\n"
+ "Chúng tôi rất vui mừng thông báo rằng bạn đã đăng ký thành công tài khoản mới tại Jacobin Store!\n\n"
+ "Chào mừng bạn đến với cửa hàng của chúng tôi và cảm ơn bạn đã chọn chúng tôi để trải nghiệm mua sắm trực tuyến.\n\n"
+ "Hãy khám phá thế giới mua sắm tuyệt vời tại Jacobin Store ngay bây giờ.\n\n"
+ "Chúc bạn có những trải nghiệm mua sắm thú vị và hài lòng!\n\n"
+ "Trân trọng, Jacobin Store.";
boolean isBodyHTML = false;

try {
MailUtilGmail.sendMail(to, from, subject, body, isBodyHTML);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

session.removeAttribute("user");
session.removeAttribute("otpSend");
}
else {
message = "Mã OTP bạn nhập không đúng!";
}
} else {
message = "";
}

req.setAttribute("message", message);
req.getRequestDispatcher(url).forward(req, resp);
}
}
16 changes: 16 additions & 0 deletions src/main/java/com/jacobin/dao/UserDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,22 @@ public static User selectUserByUserName(String userName) {
em.close();
}
}

public static User selectUserByEmail(String email) {
EntityManager em = DBUtil.getEmFactory().createEntityManager();
String qString = "SELECT u FROM User u " +
"WHERE u.email = :email";
TypedQuery<User> q = em.createQuery(qString, User.class);
q.setParameter("email", email);
try {
User user = q.getSingleResult();
return user;
} catch (NoResultException e) {
return null;
} finally {
em.close();
}
}

public static boolean checkEmailExists(String email) {
EntityManager em = DBUtil.getEmFactory().createEntityManager();
Expand Down
6 changes: 3 additions & 3 deletions src/main/webapp/WEB-INF/views/cartView.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -112,17 +112,17 @@
<div class="p-4">
<div class="form-floating mb-4">
<input type="text" class="form-control" id="ten" placeholder="Tên" name="firstName" value="${loginedUser.firstName}" required readonly>
<label for="ten">Tên<span class="red">*</span></label>
<label for="ten">Tên <span class="red">*</span></label>
</div>

<div class="form-floating mb-4">
<input type="tel" class="form-control" id="dienThoai" placeholder="Số điện thoại" name="phone" value="${loginedUser.phone}" required readonly>
<label for="dienThoai">Số điện thoại<span class="red">*</span></label>
<label for="dienThoai">Số điện thoại <span class="red">*</span></label>
</div>

<div class="form-floating mb-4">
<input type="text" class="form-control" id="diaChiKhachHang" placeholder="Địa chỉ" name="address" value="${loginedUser.address}" required>
<label for="diaChiKhachHang">Địa chỉ<span class="red">*</span></label>
<label for="diaChiKhachHang">Địa chỉ <span class="red">*</span></label>
</div>

<select name="paymentMethod" class="form-select mb-4" required>
Expand Down
Loading

0 comments on commit 8dfe073

Please sign in to comment.