From 69a48d949f146cd217bd339354c5aad1ed86e789 Mon Sep 17 00:00:00 2001 From: Lemonochrme Date: Mon, 16 Dec 2024 21:22:38 +0100 Subject: [PATCH] Refactor user authentication to use request and response; implement user login in frontend and remove script.js --- helpapp-backend/manage-services.sh | 31 +++++ .../helpapp/rest/UserServiceApplication.java | 57 +++++++- helpapp-frontend/index.html | 128 ++++++++++-------- helpapp-frontend/script.js | 69 ---------- 4 files changed, 154 insertions(+), 131 deletions(-) create mode 100755 helpapp-backend/manage-services.sh delete mode 100644 helpapp-frontend/script.js diff --git a/helpapp-backend/manage-services.sh b/helpapp-backend/manage-services.sh new file mode 100755 index 0000000..0d0f4e6 --- /dev/null +++ b/helpapp-backend/manage-services.sh @@ -0,0 +1,31 @@ +#!/bin/bash + +# List of services +services=("user-service" "request-service" "volunteer-service" "feedback-service" "administration-service") + +# Function to start services +start_services() { + for service in "${services[@]}"; do + echo "Starting $service..." + mvn spring-boot:run -pl $service & + done +} + +# Function to stop services +stop_services() { + echo "Stopping all services..." + pkill -f 'mvn spring-boot:run' + for port in {8080..8085}; do + echo "Killing process on port $port..." + fuser -k $port/tcp + done +} + +# Check command line arguments +if [ "$1" == "start" ]; then + start_services +elif [ "$1" == "stop" ]; then + stop_services +else + echo "Usage: $0 {start|stop}" +fi \ No newline at end of file diff --git a/helpapp-backend/user-service/src/main/java/insa/application/helpapp/rest/UserServiceApplication.java b/helpapp-backend/user-service/src/main/java/insa/application/helpapp/rest/UserServiceApplication.java index e28966f..8169b18 100644 --- a/helpapp-backend/user-service/src/main/java/insa/application/helpapp/rest/UserServiceApplication.java +++ b/helpapp-backend/user-service/src/main/java/insa/application/helpapp/rest/UserServiceApplication.java @@ -2,8 +2,8 @@ package insa.application.helpapp.rest; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.web.bind.annotation.*; import org.springframework.context.annotation.Bean; +import org.springframework.web.bind.annotation.*; import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @@ -55,12 +55,12 @@ public class UserServiceApplication { // Authenticate a user @PostMapping("/authenticate") - public String authenticate(@RequestParam String email, @RequestParam String password) { + public AuthResponse authenticate(@RequestBody AuthRequest authRequest) { return userDatabase.values().stream() - .filter(user -> user.getEmail().equals(email) && user.getPassword().equals(password)) + .filter(user -> user.getEmail().equals(authRequest.getEmail()) && user.getPassword().equals(authRequest.getPassword())) .findFirst() - .map(user -> "Authentication successful for user ID: " + user.getId()) - .orElse("Authentication failed"); + .map(user -> new AuthResponse(user.getId(), "Authentication successful", true)) + .orElse(new AuthResponse(null, "Authentication failed", false)); } // Update user details (excluding password) @@ -90,6 +90,52 @@ public class UserServiceApplication { return "User deleted successfully"; } + // Data transfer objects + static class AuthRequest { + private String email; + private String password; + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + } + + static class AuthResponse { + private Long userId; + private String message; + private boolean success; + + public AuthResponse(Long userId, String message, boolean success) { + this.userId = userId; + this.message = message; + this.success = success; + } + + public Long getUserId() { + return userId; + } + + public String getMessage() { + return message; + } + + public boolean isSuccess() { + return success; + } + } + // User entity static class User { private Long id; @@ -98,7 +144,6 @@ public class UserServiceApplication { private String password; private String role; // REQUESTER, VOLUNTEER, ADMIN - // Getters and setters public Long getId() { return id; } diff --git a/helpapp-frontend/index.html b/helpapp-frontend/index.html index 77e6dcb..693981f 100644 --- a/helpapp-frontend/index.html +++ b/helpapp-frontend/index.html @@ -4,61 +4,22 @@ HelpApp -

HelpApp

+
+

Connexion

+
+ + + +
+
+
+

Créer un compte utilisateur

@@ -83,14 +44,69 @@
- -
-

Voir les demandes d'aide

- -
    -
    - + diff --git a/helpapp-frontend/script.js b/helpapp-frontend/script.js deleted file mode 100644 index 2da4fbf..0000000 --- a/helpapp-frontend/script.js +++ /dev/null @@ -1,69 +0,0 @@ -const API_BASE = "http://localhost"; - -document.addEventListener("DOMContentLoaded", () => { - // User Registration - const registerForm = document.getElementById("register-form"); - const registerResponse = document.getElementById("register-response"); - - registerForm.addEventListener("submit", async (e) => { - e.preventDefault(); - const name = document.getElementById("name").value; - const email = document.getElementById("email").value; - const password = document.getElementById("password").value; - const role = document.getElementById("role").value; - - try { - const response = await fetch(`${API_BASE}:8083/users`, { - method: "POST", - headers: { "Content-Type": "application/json" }, - body: JSON.stringify({ name, email, password, role }), - }); - const result = await response.json(); - registerResponse.textContent = `Utilisateur créé : ID ${result.id}`; - } catch (error) { - registerResponse.textContent = "Erreur lors de la création de l'utilisateur."; - } - }); - - // Create Help Request - const requestForm = document.getElementById("request-form"); - const requestResponse = document.getElementById("request-response"); - - requestForm.addEventListener("submit", async (e) => { - e.preventDefault(); - const userId = document.getElementById("user-id").value; - const details = document.getElementById("details").value; - - try { - const response = await fetch(`${API_BASE}:8082/requests`, { - method: "POST", - headers: { "Content-Type": "application/json" }, - body: JSON.stringify({ userId, details }), - }); - const result = await response.json(); - requestResponse.textContent = `Demande créée : ID ${result.id}`; - } catch (error) { - requestResponse.textContent = "Erreur lors de la création de la demande."; - } - }); - - // Fetch Help Requests - const fetchRequestsButton = document.getElementById("fetch-requests"); - const requestList = document.getElementById("request-list"); - - fetchRequestsButton.addEventListener("click", async () => { - try { - const response = await fetch(`${API_BASE}:8082/requests`); - const requests = await response.json(); - - requestList.innerHTML = ""; // Clear existing list - requests.forEach((req) => { - const li = document.createElement("li"); - li.textContent = `ID: ${req.id}, Détails: ${req.details}, Statut: ${req.status}`; - requestList.appendChild(li); - }); - } catch (error) { - requestList.innerHTML = "
  • Erreur lors de la récupération des demandes.
  • "; - } - }); -});