This repository has been archived by the owner on Dec 31, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
77c0b7d
commit aaf0a6c
Showing
8 changed files
with
485 additions
and
0 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
src/main/java/io/hardingadonis/miu/controller/web/ForgotChangePasswordServlet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package io.hardingadonis.miu.controller.web; | ||
|
||
import io.hardingadonis.miu.model.*; | ||
import io.hardingadonis.miu.services.*; | ||
import java.io.*; | ||
import javax.servlet.*; | ||
import javax.servlet.annotation.*; | ||
import javax.servlet.http.*; | ||
import org.json.simple.*; | ||
|
||
@WebServlet(name = "ForgotChangePasswordServlet", urlPatterns = {"/forgot-change-password"}) | ||
public class ForgotChangePasswordServlet 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) { | ||
response.sendRedirect("home"); | ||
return; | ||
} | ||
|
||
request.getRequestDispatcher("/view/web/forgot-change-password.jsp").forward(request, response); | ||
} | ||
|
||
@Override | ||
protected void doPost(HttpServletRequest request, HttpServletResponse response) | ||
throws ServletException, IOException { | ||
try { | ||
String email = request.getParameter("email"); | ||
String password = request.getParameter("password"); | ||
|
||
User user = Singleton.userDAO.get(email); | ||
|
||
if (user != null) { | ||
user.setHashedPassword(Hash.SHA256(password)); | ||
Singleton.userDAO.update(user); | ||
|
||
JSONObject jsonResponse = new JSONObject(); | ||
jsonResponse.put("status", "success"); | ||
jsonResponse.put("message", "Change password successfully"); | ||
|
||
response.setContentType("application/json"); | ||
response.getWriter().write(jsonResponse.toString()); | ||
|
||
response.setStatus(HttpServletResponse.SC_OK); | ||
} | ||
|
||
} catch (NumberFormatException ex) { | ||
System.err.println(ex.getMessage()); | ||
} | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
src/main/java/io/hardingadonis/miu/controller/web/ForgotPasswordServlet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package io.hardingadonis.miu.controller.web; | ||
|
||
import io.hardingadonis.miu.model.*; | ||
import io.hardingadonis.miu.services.*; | ||
import java.io.*; | ||
import javax.servlet.*; | ||
import javax.servlet.annotation.*; | ||
import javax.servlet.http.*; | ||
|
||
@WebServlet(name = "ForgotPasswordServlet", urlPatterns = {"/forgot-password"}) | ||
public class ForgotPasswordServlet 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) { | ||
response.sendRedirect("home"); | ||
return; | ||
} | ||
|
||
String code = request.getParameter("code"); | ||
|
||
if ((code != null) && (code.equals((String) session.getAttribute("code")))) { | ||
session.removeAttribute("code"); | ||
} | ||
|
||
request.getRequestDispatcher("/view/web/forgot-password.jsp").forward(request, response); | ||
} | ||
|
||
@Override | ||
protected void doPost(HttpServletRequest request, HttpServletResponse response) | ||
throws ServletException, IOException { | ||
request.setCharacterEncoding("UTF-8"); | ||
response.setContentType("text/html; charset=UTF-8"); | ||
|
||
String email = request.getParameter("email"); | ||
|
||
User user = Singleton.userDAO.get(email); | ||
|
||
if (user != null) { | ||
String code = Hash.SHA256(email + System.currentTimeMillis()); | ||
|
||
Singleton.email.sendForgotPasswordEmail(user, code, request); | ||
|
||
response.sendRedirect("forgot-password?sent=true"); | ||
return; | ||
} | ||
|
||
String errorMsg = "Tài khoản không tồn tại!"; | ||
|
||
request.setAttribute("errorMsg", errorMsg); | ||
|
||
this.doGet(request, response); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
* { | ||
font-family: 'Poppins', sans-serif; | ||
} | ||
|
||
body { | ||
background-image: url('../../images/covers/d8c447f52ed8adc4ec394b0d1a0b7cda88dc9c90b6e3795796c90dd27267214b.jpg'); | ||
background-size: cover; | ||
background-position: center center; | ||
background-repeat: no-repeat; | ||
background-attachment: fixed; | ||
backdrop-filter: blur(5px); | ||
} | ||
|
||
.main { | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
min-height: 100vh; | ||
} | ||
|
||
.forgot-password-container { | ||
width: 65%; | ||
min-height: 400px; | ||
border-radius: 5px; | ||
background: #ffffff; | ||
box-shadow: 5px 5px 10px 1px rgba(0, 0, 0, 0.2); | ||
} | ||
|
||
header { | ||
font-weight: 700; | ||
font-size: 25px; | ||
text-align: center; | ||
margin-bottom: 45px; | ||
} | ||
|
||
#submit-form { | ||
align-items: center; | ||
} | ||
|
||
.submit { | ||
margin: 0; | ||
border: none; | ||
outline: none; | ||
height: 45px; | ||
width: 50%; | ||
background: #ececec; | ||
border-radius: 5px; | ||
transition: .2s; | ||
} | ||
|
||
.submit:hover { | ||
background: rgba(33, 37, 41, 0.9); | ||
color: #ffffff; | ||
} | ||
|
||
.toggle-password { | ||
float: right; | ||
cursor: pointer; | ||
margin-right: 10px; | ||
margin-top: -27px; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
* { | ||
font-family: 'Poppins', sans-serif; | ||
} | ||
|
||
body { | ||
background-image: url('../../images/covers/d8c447f52ed8adc4ec394b0d1a0b7cda88dc9c90b6e3795796c90dd27267214b.jpg'); | ||
background-size: cover; | ||
background-position: center center; | ||
background-repeat: no-repeat; | ||
background-attachment: fixed; | ||
backdrop-filter: blur(5px); | ||
} | ||
|
||
.main { | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
min-height: 100vh; | ||
} | ||
|
||
.forgot-password-container { | ||
width: 65%; | ||
min-height: 400px; | ||
border-radius: 5px; | ||
background: #ffffff; | ||
box-shadow: 5px 5px 10px 1px rgba(0, 0, 0, 0.2); | ||
} | ||
|
||
header { | ||
font-weight: 700; | ||
font-size: 25px; | ||
text-align: center; | ||
margin-bottom: 45px; | ||
} | ||
|
||
#submit-form { | ||
align-items: center; | ||
} | ||
|
||
.submit { | ||
margin: 0; | ||
border: none; | ||
outline: none; | ||
height: 45px; | ||
width: 50%; | ||
background: #ececec; | ||
border-radius: 5px; | ||
transition: .2s; | ||
} | ||
|
||
.submit:hover { | ||
background: rgba(33, 37, 41, 0.9); | ||
color: #ffffff; | ||
} |
105 changes: 105 additions & 0 deletions
105
src/main/webapp/assets/js/web/forgotChangePasswordHandler.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
const changePasswordForm = document.getElementById('change-password-form'); | ||
const emailInput = document.getElementById('email'); | ||
const passwordInput = document.getElementById('password'); | ||
const confirmPasswordInput = document.getElementById('comfirm-password'); | ||
const errorMessage = document.getElementById('error-message'); | ||
|
||
if (changePasswordForm) { | ||
changePasswordForm.addEventListener('submit', function (event) { | ||
event.preventDefault(); | ||
|
||
if (!isStrongPassword(passwordInput.value)) { | ||
errorMessage.textContent = 'Mật khẩu mới phải có ít nhất 6 ký tự, trong đó có ít nhất 1 chữ số, 1 chữ cái viết hoa, 1 ký tự đặc biệt!'; | ||
passwordInput.focus(); | ||
return; | ||
} | ||
|
||
if (!isPasswordMatch(passwordInput.value, confirmPasswordInput.value)) { | ||
errorMessage.textContent = 'Mật khẩu mới không khớp!'; | ||
confirmNewPasswordInput.focus(); | ||
return; | ||
} | ||
|
||
const url = 'forgot-change-password?email=' + emailInput.textContent + '&password=' + passwordInput.value; | ||
|
||
$.ajax({ | ||
url: url, | ||
type: "POST", | ||
dataType: "json", | ||
success: function (data) { | ||
if (data && data.status === "success") { | ||
Swal.fire({ | ||
title: "Thành công!", | ||
text: "Bạn đã đổi mật khẩu thành công!", | ||
icon: "success" | ||
}).then((result) => { | ||
if (result.isConfirmed) { | ||
window.location.href = 'login'; | ||
} | ||
}); | ||
} else { | ||
Swal.fire({ | ||
title: "Lỗi!", | ||
text: "Không thể đổi mật khẩu. Vui lòng thử lại!", | ||
icon: "error" | ||
}); | ||
} | ||
}, | ||
error: function (xhr, status, error) { | ||
Swal.fire({ | ||
title: "Lỗi!", | ||
text: "Đã xảy ra lỗi trong quá trình xử lý yêu cầu. Vui lòng thử lại!", | ||
icon: "error" | ||
}); | ||
} | ||
}); | ||
|
||
}); | ||
|
||
passwordInput.addEventListener('input', function () { | ||
removeSpaces(passwordInput); | ||
}); | ||
|
||
confirmPasswordInput.addEventListener('input', function () { | ||
removeSpaces(confirmPasswordInput); | ||
}); | ||
|
||
function removeSpaces(input) { | ||
input.value = input.value.replace(/\s/g, ''); | ||
} | ||
|
||
function isPasswordMatch(password, confirmPassword) { | ||
return password === confirmPassword; | ||
} | ||
|
||
function isStrongPassword(password) { | ||
if (password.length < 6) { | ||
return false; | ||
} | ||
|
||
if (!/\d/.test(password)) { | ||
return false; | ||
} | ||
|
||
if (!/[!@#$%^&*()_+{}\[\]:;<>,.?~\\/-]/.test(password)) { | ||
return false; | ||
} | ||
|
||
if (!/[A-Z]/.test(password)) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
$(".toggle-password").click(function () { | ||
$(this).toggleClass("fa-eye fa-eye-slash"); | ||
input = $(this).parent().find("input"); | ||
|
||
if (input.attr("type") == "password") { | ||
input.attr("type", "text"); | ||
} else { | ||
input.attr("type", "password"); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
const forgotForm = document.getElementById('forgot-password-form'); | ||
const emailInput = document.getElementById('email'); | ||
const errorMessage = document.getElementById('error-message'); | ||
|
||
if (forgotForm) { | ||
|
||
forgotForm.addEventListener('submit', function (event) { | ||
event.preventDefault(); | ||
|
||
const emailValue = emailInput.value; | ||
|
||
if (!isValidEmail(emailValue)) { | ||
errorMessage.textContent = 'Email không hợp lệ!'; | ||
emailInput.focus(); | ||
return; | ||
} | ||
|
||
this.submit(); | ||
}); | ||
|
||
emailInput.addEventListener('input', function () { | ||
removeSpaces(emailInput); | ||
}); | ||
|
||
function removeSpaces(input) { | ||
input.value = input.value.replace(/\s/g, ''); | ||
} | ||
|
||
function isValidEmail(email) { | ||
return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email); | ||
} | ||
} |
Oops, something went wrong.