mirror of
https://github.com/kmitresse/Cards-Rush.git
synced 2026-05-13 17:11:49 +00:00
draft - devweb - login
This commit is contained in:
@@ -0,0 +1,29 @@
|
|||||||
|
package uppa.project.pojo.json;
|
||||||
|
|
||||||
|
public class LoginRequest {
|
||||||
|
|
||||||
|
private String username;
|
||||||
|
private String password;
|
||||||
|
|
||||||
|
public LoginRequest() {}
|
||||||
|
public LoginRequest(String username, String password) {
|
||||||
|
this.username = username;
|
||||||
|
this.password = password;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUsername() {
|
||||||
|
return username;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUsername(String username) {
|
||||||
|
this.username = username;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPassword() {
|
||||||
|
return password;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPassword(String password) {
|
||||||
|
this.password = password;
|
||||||
|
}
|
||||||
|
}
|
||||||
+16
-17
@@ -14,6 +14,7 @@ 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.BufferedReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.PrintWriter;
|
import java.io.PrintWriter;
|
||||||
import uppa.project.dao.DAO;
|
import uppa.project.dao.DAO;
|
||||||
@@ -21,6 +22,7 @@ import uppa.project.dao.DAOException;
|
|||||||
import uppa.project.dao.jpa.Game_JPA_DAO_Factory;
|
import uppa.project.dao.jpa.Game_JPA_DAO_Factory;
|
||||||
import uppa.project.pojo.User;
|
import uppa.project.pojo.User;
|
||||||
import uppa.project.pojo.json.ErrorResponse;
|
import uppa.project.pojo.json.ErrorResponse;
|
||||||
|
import uppa.project.pojo.json.LoginRequest;
|
||||||
import uppa.project.pojo.json.LoginResponse;
|
import uppa.project.pojo.json.LoginResponse;
|
||||||
import uppa.project.utils.HttpRequestUtils;
|
import uppa.project.utils.HttpRequestUtils;
|
||||||
|
|
||||||
@@ -37,24 +39,18 @@ public class AuthLoginServlet extends HttpServlet {
|
|||||||
response.setCharacterEncoding("UTF-8");
|
response.setCharacterEncoding("UTF-8");
|
||||||
PrintWriter out = response.getWriter();
|
PrintWriter out = response.getWriter();
|
||||||
|
|
||||||
// Convert the string to a JSON object
|
// Get the json in the request body
|
||||||
JsonObject jsonBody;
|
StringBuilder sb = new StringBuilder();
|
||||||
try {
|
BufferedReader reader = request.getReader();
|
||||||
String requestBody = HttpRequestUtils.getRequestBody(request);
|
String line;
|
||||||
jsonBody = JsonParser.parseString(requestBody).getAsJsonObject();
|
while((line = reader.readLine()) != null) {
|
||||||
} catch (Exception e) {
|
sb.append(line);
|
||||||
int STATUS = 400;
|
|
||||||
|
|
||||||
ErrorResponse error = new ErrorResponse(STATUS, "Bad Request", "Invalid JSON");
|
|
||||||
response.setStatus(STATUS);
|
|
||||||
out.println(gson.toJson(error));
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
String requestBody = sb.toString();
|
||||||
|
LoginRequest loginRequest = gson.fromJson(requestBody, LoginRequest.class);
|
||||||
|
|
||||||
// Check if the username and password are present
|
// Check if the username and password are present
|
||||||
JsonElement username = jsonBody.get("username");
|
if (loginRequest.getUsername() == null || loginRequest.getPassword() == null) {
|
||||||
JsonElement password = jsonBody.get("password");
|
|
||||||
if (username == null || password == null) {
|
|
||||||
int STATUS = 400;
|
int STATUS = 400;
|
||||||
ErrorResponse error = new ErrorResponse(STATUS, "Bad Request", "Username and password are required");
|
ErrorResponse error = new ErrorResponse(STATUS, "Bad Request", "Username and password are required");
|
||||||
response.setStatus(STATUS);
|
response.setStatus(STATUS);
|
||||||
@@ -63,7 +59,7 @@ public class AuthLoginServlet extends HttpServlet {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Get User from database matching the username and password
|
// Get User from database matching the username and password
|
||||||
User user = AuthLoginServlet.loginUser(username.getAsString(), password.getAsString());
|
User user = AuthLoginServlet.loginUser(loginRequest);
|
||||||
if (user == null) {
|
if (user == null) {
|
||||||
int STATUS = 401;
|
int STATUS = 401;
|
||||||
ErrorResponse error = new ErrorResponse(STATUS, "Unauthorized", "Invalid username or password");
|
ErrorResponse error = new ErrorResponse(STATUS, "Unauthorized", "Invalid username or password");
|
||||||
@@ -83,7 +79,10 @@ public class AuthLoginServlet extends HttpServlet {
|
|||||||
out.flush();
|
out.flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static User loginUser(String username, String password) {
|
private static User loginUser(LoginRequest loginRequest) {
|
||||||
|
String username = loginRequest.getUsername();
|
||||||
|
String password = loginRequest.getPassword();
|
||||||
|
|
||||||
Game_JPA_DAO_Factory factory = new Game_JPA_DAO_Factory();
|
Game_JPA_DAO_Factory factory = new Game_JPA_DAO_Factory();
|
||||||
try {
|
try {
|
||||||
DAO<User> userDao = factory.getDAOUser();
|
DAO<User> userDao = factory.getDAOUser();
|
||||||
|
|||||||
@@ -15,11 +15,10 @@ loginForm.addEventListener("submit", (event) => {
|
|||||||
body: JSON.stringify(data),
|
body: JSON.stringify(data),
|
||||||
method,
|
method,
|
||||||
})
|
})
|
||||||
.then(res => res.json())
|
.then(res => console.log(res))
|
||||||
.then(data => {
|
//.then(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))
|
||||||
;
|
;
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user