mirror of
https://github.com/kmitresse/Cards-Rush.git
synced 2026-05-16 09:05:28 +00:00
feat(DevWeb): Projet - Login page, Login Api
This commit is contained in:
@@ -1,27 +0,0 @@
|
||||
package uppa.project.servlet;
|
||||
|
||||
import java.io.*;
|
||||
import jakarta.servlet.http.*;
|
||||
import jakarta.servlet.annotation.*;
|
||||
|
||||
@WebServlet(name = "helloServlet", value = "/hello-servlet")
|
||||
public class HelloServlet extends HttpServlet {
|
||||
private String message;
|
||||
|
||||
public void init() {
|
||||
message = "Hello World!";
|
||||
}
|
||||
|
||||
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
|
||||
response.setContentType("text/html");
|
||||
|
||||
// Hello
|
||||
PrintWriter out = response.getWriter();
|
||||
out.println("<html><body>");
|
||||
out.println("<h1>" + message + "</h1>");
|
||||
out.println("</body></html>");
|
||||
}
|
||||
|
||||
public void destroy() {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
package uppa.project.servlet;
|
||||
|
||||
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.servlet.json.ErrorApi;
|
||||
import uppa.project.pojo.User;
|
||||
import uppa.project.servlet.utils.RequestUtils;
|
||||
|
||||
@WebServlet(name = "loginApiServlet", value = "/api/login")
|
||||
public class LoginServlet 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 = RequestUtils.getRequestBody(request);
|
||||
jsonBody = JsonParser.parseString(requestBody).getAsJsonObject();
|
||||
} catch (Exception e) {
|
||||
int STATUS = 400;
|
||||
|
||||
ErrorApi error = new ErrorApi(STATUS, "Bad Request", "Invalid JSON");
|
||||
response.setStatus(STATUS);
|
||||
out.println(error.toJson());
|
||||
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;
|
||||
|
||||
ErrorApi error = new ErrorApi(STATUS, "Bad Request", "Username and password are required");
|
||||
response.setStatus(STATUS);
|
||||
out.println(error.toJson());
|
||||
return;
|
||||
}
|
||||
|
||||
// Get User from database matching the username and password
|
||||
User user = LoginServlet.loginUser(username.getAsString(), password.getAsString());
|
||||
if (user == null) {
|
||||
int STATUS = 401;
|
||||
ErrorApi error = new ErrorApi(STATUS, "Unauthorized", "Invalid username or password");
|
||||
|
||||
response.setStatus(STATUS);
|
||||
out.println(error.toJson());
|
||||
return;
|
||||
}
|
||||
|
||||
// Return the user as JSON
|
||||
String json = gson.toJson(user);
|
||||
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() {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package uppa.project.servlet.json;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
|
||||
public class ErrorApi {
|
||||
private final int status;
|
||||
private final String error;
|
||||
private final String message;
|
||||
|
||||
public ErrorApi(int status, String error, String message) {
|
||||
this.status = status;
|
||||
this.error = error;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public int getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public String getError() {
|
||||
return error;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public String toJson() {
|
||||
Gson gson = new Gson();
|
||||
return gson.toJson(this);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package uppa.project.servlet.utils;
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
|
||||
public class RequestUtils {
|
||||
public static String getRequestBody(HttpServletRequest request) throws IOException {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
BufferedReader reader = request.getReader();
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
sb.append(line);
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user