From 0a64834db42a4eb2e5d6a96b21e36c98240cea9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Minh=20V=C6=B0=C6=A1ng?= Date: Thu, 7 Dec 2023 17:08:08 +0700 Subject: [PATCH] Add Verify Servlet and its jsp page --- .../miu/controller/web/VerifyServlet.java | 69 +++++++++++++++++++ src/main/webapp/view/web/verify.jsp | 50 ++++++++++++++ 2 files changed, 119 insertions(+) create mode 100644 src/main/java/io/hardingadonis/miu/controller/web/VerifyServlet.java create mode 100644 src/main/webapp/view/web/verify.jsp diff --git a/src/main/java/io/hardingadonis/miu/controller/web/VerifyServlet.java b/src/main/java/io/hardingadonis/miu/controller/web/VerifyServlet.java new file mode 100644 index 00000000..c81aef62 --- /dev/null +++ b/src/main/java/io/hardingadonis/miu/controller/web/VerifyServlet.java @@ -0,0 +1,69 @@ +package io.hardingadonis.miu.controller.web; + +import io.hardingadonis.miu.model.*; +import io.hardingadonis.miu.model.detail.*; +import io.hardingadonis.miu.services.*; +import java.io.*; +import javax.servlet.*; +import javax.servlet.annotation.*; +import javax.servlet.http.*; + +@WebServlet(name = "VerifyServlet", urlPatterns = {"/verify"}) +public class VerifyServlet extends HttpServlet { + + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + request.setCharacterEncoding("UTF-8"); + response.setContentType("text/html; charset=UTF-8"); + + HttpSession session = request.getSession(); + + User user = (User) session.getAttribute("user"); + + if ((user == null) || (user.getStatus() == UserStatus.ACTIVATE)) { + response.sendRedirect("home"); + return; + } + + String email = request.getParameter("email"); + + String msg = null; + + if ((email == null) || (email.isEmpty())) { + String hashedStr = Hash.SHA256(email + System.currentTimeMillis()); + + session.setAttribute("hashed_str", hashedStr); + + Singleton.email.sendVerifyEmail(user, hashedStr, request); + + msg = "Bạn đã nhận được một email xác thực tài khoản. Vui lòng kiểm tra email!"; + } else { + if (user.getEmail().equals(email)) { + String hashedStrParameter = request.getParameter("code"); + String hashedStrSession = (String) session.getAttribute("hashed_str"); + + if ((hashedStrParameter != null) && (hashedStrSession != null) && (hashedStrParameter.equals(hashedStrSession))) { + user.setStatus(UserStatus.ACTIVATE); + + session.setAttribute("hashed_str", null); + + request.setAttribute("success", true); + + Singleton.userDAO.update(user); + Singleton.email.sendWelcomeEmail(user); + + msg = "Xác thực thành công!"; + } else { + msg = "Xác thực thất bại!"; + } + } else { + msg = "Xác thực thất bại!"; + } + } + + request.setAttribute("msg", msg); + + request.getRequestDispatcher("/view/web/verify.jsp").forward(request, response); + } +} diff --git a/src/main/webapp/view/web/verify.jsp b/src/main/webapp/view/web/verify.jsp new file mode 100644 index 00000000..a5a429a9 --- /dev/null +++ b/src/main/webapp/view/web/verify.jsp @@ -0,0 +1,50 @@ +<%@ page contentType="text/html" pageEncoding="UTF-8" %> +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> + + + + + + + + + + + + + + + Miu Shop | Xác thực + + + + <%@include file="common/_header.jsp" %> + +
+
+
+
+ Xác thực tài khoản +
+
+
+ ${msg} +
+
+ + + +
+
+
+
+ + <%@include file="common/_footer.jsp" %> + + + + + + \ No newline at end of file