mirror of
https://github.com/kmitresse/Cards-Rush.git
synced 2026-07-09 13:27:45 +00:00
fix(DevWeb): Correct redirection from login
This commit is contained in:
@@ -7,29 +7,16 @@
|
|||||||
package uppa.project.servlet;
|
package uppa.project.servlet;
|
||||||
|
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.google.gson.JsonElement;
|
|
||||||
import com.google.gson.JsonObject;
|
|
||||||
import com.google.gson.JsonParser;
|
|
||||||
import jakarta.servlet.ServletException;
|
import jakarta.servlet.ServletException;
|
||||||
import jakarta.servlet.annotation.WebServlet;
|
import jakarta.servlet.annotation.WebServlet;
|
||||||
import jakarta.servlet.http.HttpServlet;
|
import jakarta.servlet.http.HttpServlet;
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
import jakarta.servlet.http.HttpServletResponse;
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.PrintWriter;
|
|
||||||
import uppa.project.dao.DAO;
|
|
||||||
import uppa.project.dao.DAOException;
|
|
||||||
import uppa.project.dao.jpa.Game_JPA_DAO_Factory;
|
|
||||||
import uppa.project.pojo.json.ErrorResponse;
|
|
||||||
import uppa.project.pojo.User;
|
|
||||||
import uppa.project.pojo.json.LoginResponse;
|
|
||||||
import uppa.project.utils.HttpRequestUtils;
|
|
||||||
|
|
||||||
@WebServlet(name = "loginServlet", value = "/login")
|
@WebServlet(name = "loginServlet", value = "/login")
|
||||||
public class LoginServlet extends HttpServlet {
|
public class LoginServlet extends HttpServlet {
|
||||||
|
|
||||||
private final Gson gson = new Gson();
|
|
||||||
|
|
||||||
public void init() {
|
public void init() {
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -42,73 +29,6 @@ public class LoginServlet extends HttpServlet {
|
|||||||
request.getRequestDispatcher("/WEB-INF/views/login.jsp").forward(request, response);
|
request.getRequestDispatcher("/WEB-INF/views/login.jsp").forward(request, response);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
|
|
||||||
response.setContentType("application/json");
|
|
||||||
response.setCharacterEncoding("UTF-8");
|
|
||||||
PrintWriter out = response.getWriter();
|
|
||||||
|
|
||||||
// Convert the string to a JSON object
|
|
||||||
JsonObject jsonBody;
|
|
||||||
try {
|
|
||||||
String requestBody = HttpRequestUtils.getRequestBody(request);
|
|
||||||
jsonBody = JsonParser.parseString(requestBody).getAsJsonObject();
|
|
||||||
} catch (Exception e) {
|
|
||||||
int STATUS = 400;
|
|
||||||
|
|
||||||
ErrorResponse error = new ErrorResponse(STATUS, "Bad Request", "Invalid JSON");
|
|
||||||
response.setStatus(STATUS);
|
|
||||||
out.println(gson.toJson(error));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if the username and password are present
|
|
||||||
JsonElement username = jsonBody.get("username");
|
|
||||||
JsonElement password = jsonBody.get("password");
|
|
||||||
if (username == null || password == null) {
|
|
||||||
int STATUS = 400;
|
|
||||||
|
|
||||||
ErrorResponse error = new ErrorResponse(STATUS, "Bad Request", "Username and password are required");
|
|
||||||
response.setStatus(STATUS);
|
|
||||||
out.println(gson.toJson(error));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get User from database matching the username and password
|
|
||||||
User user = LoginServlet.loginUser(username.getAsString(), password.getAsString());
|
|
||||||
if (user == null) {
|
|
||||||
int STATUS = 401;
|
|
||||||
ErrorResponse error = new ErrorResponse(STATUS, "Unauthorized", "Invalid username or password");
|
|
||||||
|
|
||||||
response.setStatus(STATUS);
|
|
||||||
out.println(gson.toJson(error));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set the user in the session
|
|
||||||
request.getSession().setAttribute("user", user);
|
|
||||||
|
|
||||||
// Return the user as JSON
|
|
||||||
LoginResponse loginResponse = new LoginResponse(200, user, request.getContextPath() + "/main-menu");
|
|
||||||
String json = gson.toJson(loginResponse);
|
|
||||||
out.println(json);
|
|
||||||
out.flush();
|
|
||||||
}
|
|
||||||
|
|
||||||
private static User loginUser(String username, String password) {
|
|
||||||
Game_JPA_DAO_Factory factory = new Game_JPA_DAO_Factory();
|
|
||||||
try {
|
|
||||||
DAO<User> userDao = factory.getDAOUser();
|
|
||||||
User[] users = userDao.findByField("username", username);
|
|
||||||
|
|
||||||
for (User user : users) {
|
|
||||||
if (user.verifyPassword(password)) return user;
|
|
||||||
}
|
|
||||||
} catch (DAOException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void destroy() {
|
public void destroy() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,104 @@
|
|||||||
|
/*
|
||||||
|
* LoginServlet.java, 20/03/2024
|
||||||
|
* UPPA M1 TI 2023-2024
|
||||||
|
* Pas de copyright, aucun droits
|
||||||
|
*/
|
||||||
|
|
||||||
|
package uppa.project.servlet.api.auth;
|
||||||
|
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
import com.google.gson.JsonElement;
|
||||||
|
import com.google.gson.JsonObject;
|
||||||
|
import com.google.gson.JsonParser;
|
||||||
|
import jakarta.servlet.annotation.WebServlet;
|
||||||
|
import jakarta.servlet.http.HttpServlet;
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.PrintWriter;
|
||||||
|
import uppa.project.dao.DAO;
|
||||||
|
import uppa.project.dao.DAOException;
|
||||||
|
import uppa.project.dao.jpa.Game_JPA_DAO_Factory;
|
||||||
|
import uppa.project.pojo.User;
|
||||||
|
import uppa.project.pojo.json.ErrorResponse;
|
||||||
|
import uppa.project.pojo.json.LoginResponse;
|
||||||
|
import uppa.project.utils.HttpRequestUtils;
|
||||||
|
|
||||||
|
@WebServlet(name = "authLoginServlet", value = "/api/auth/login")
|
||||||
|
public class AuthLoginServlet extends HttpServlet {
|
||||||
|
|
||||||
|
private final Gson gson = new Gson();
|
||||||
|
|
||||||
|
public void init() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
|
||||||
|
response.setContentType("application/json");
|
||||||
|
response.setCharacterEncoding("UTF-8");
|
||||||
|
PrintWriter out = response.getWriter();
|
||||||
|
|
||||||
|
// Convert the string to a JSON object
|
||||||
|
JsonObject jsonBody;
|
||||||
|
try {
|
||||||
|
String requestBody = HttpRequestUtils.getRequestBody(request);
|
||||||
|
jsonBody = JsonParser.parseString(requestBody).getAsJsonObject();
|
||||||
|
} catch (Exception e) {
|
||||||
|
int STATUS = 400;
|
||||||
|
|
||||||
|
ErrorResponse error = new ErrorResponse(STATUS, "Bad Request", "Invalid JSON");
|
||||||
|
response.setStatus(STATUS);
|
||||||
|
out.println(gson.toJson(error));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if the username and password are present
|
||||||
|
JsonElement username = jsonBody.get("username");
|
||||||
|
JsonElement password = jsonBody.get("password");
|
||||||
|
if (username == null || password == null) {
|
||||||
|
int STATUS = 400;
|
||||||
|
|
||||||
|
ErrorResponse error = new ErrorResponse(STATUS, "Bad Request", "Username and password are required");
|
||||||
|
response.setStatus(STATUS);
|
||||||
|
out.println(gson.toJson(error));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get User from database matching the username and password
|
||||||
|
User user = AuthLoginServlet.loginUser(username.getAsString(), password.getAsString());
|
||||||
|
if (user == null) {
|
||||||
|
int STATUS = 401;
|
||||||
|
ErrorResponse error = new ErrorResponse(STATUS, "Unauthorized", "Invalid username or password");
|
||||||
|
|
||||||
|
response.setStatus(STATUS);
|
||||||
|
out.println(gson.toJson(error));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set the user in the session
|
||||||
|
request.getSession().setAttribute("user", user);
|
||||||
|
|
||||||
|
// Return the user as JSON
|
||||||
|
LoginResponse loginResponse = new LoginResponse(200, user, request.getContextPath() + "/main-menu");
|
||||||
|
String json = gson.toJson(loginResponse);
|
||||||
|
out.println(json);
|
||||||
|
out.flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static User loginUser(String username, String password) {
|
||||||
|
Game_JPA_DAO_Factory factory = new Game_JPA_DAO_Factory();
|
||||||
|
try {
|
||||||
|
DAO<User> userDao = factory.getDAOUser();
|
||||||
|
User[] users = userDao.findByField("username", username);
|
||||||
|
|
||||||
|
for (User user : users) {
|
||||||
|
if (user.verifyPassword(password)) return user;
|
||||||
|
}
|
||||||
|
} catch (DAOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void destroy() {
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,7 +1,8 @@
|
|||||||
const loginForm = document.getElementById("loginForm");
|
const loginForm = document.getElementById("login-form");
|
||||||
|
|
||||||
loginForm.addEventListener("submit", (event) => {
|
loginForm.addEventListener("submit", (event) => {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
|
|
||||||
const formData = new FormData(loginForm);
|
const formData = new FormData(loginForm);
|
||||||
const data = {};
|
const data = {};
|
||||||
formData.forEach((value, key) => data[key] = value);
|
formData.forEach((value, key) => data[key] = value);
|
||||||
@@ -17,7 +18,7 @@ loginForm.addEventListener("submit", (event) => {
|
|||||||
.then(res => res.json())
|
.then(res => res.json())
|
||||||
.then(data => {
|
.then(data => {
|
||||||
console.log(data);
|
console.log(data);
|
||||||
if (data.status === 200) window.location.href = data.redirect;
|
// if (data.status === 200) window.location.href = data.redirect;
|
||||||
})
|
})
|
||||||
.catch(error => console.error("Error:", error))
|
.catch(error => console.error("Error:", error))
|
||||||
;
|
;
|
||||||
|
|||||||
@@ -3,7 +3,6 @@
|
|||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<title>Login</title>
|
<title>Login</title>
|
||||||
<script defer type="text/javascript"><%@include file="../static/js/login.js" %></script>
|
|
||||||
<style><%@include file="../static/css/login.css" %></style>
|
<style><%@include file="../static/css/login.css" %></style>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
@@ -15,7 +14,7 @@
|
|||||||
<div class="flex-column login-gap">
|
<div class="flex-column login-gap">
|
||||||
<div>
|
<div>
|
||||||
<h1>Login</h1>
|
<h1>Login</h1>
|
||||||
<form id="login-form" action="${pageContext.request.contextPath}/login" method="POST">
|
<form id="login-form" action="${pageContext.request.contextPath}/api/auth/login" method="POST">
|
||||||
|
|
||||||
<label id="username-label" for="username">Username:</label>
|
<label id="username-label" for="username">Username:</label>
|
||||||
<input type="text" id="username" name="username" required>
|
<input type="text" id="username" name="username" required>
|
||||||
@@ -36,5 +35,7 @@
|
|||||||
</section>
|
</section>
|
||||||
</main>
|
</main>
|
||||||
|
|
||||||
|
<script defer type="text/javascript"><%@include file="../static/js/login.js" %></script>
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
Reference in New Issue
Block a user