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.
Merge pull request #77 from hardingadonis/vuong_forgot_password
Vương: Forgot password functionally
- Loading branch information
Showing
12 changed files
with
515 additions
and
3 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
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
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
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; | ||
} |
Oops, something went wrong.