mirror of
https://github.com/Lemonochrme/service-architecture.git
synced 2025-06-08 21:50:50 +02:00
Refactor user authentication to use request and response; implement user login in frontend and remove script.js
This commit is contained in:
parent
88abf1ad9d
commit
69a48d949f
4 changed files with 154 additions and 131 deletions
31
helpapp-backend/manage-services.sh
Executable file
31
helpapp-backend/manage-services.sh
Executable file
|
@ -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
|
|
@ -2,8 +2,8 @@ package insa.application.helpapp.rest;
|
||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
import org.springframework.web.bind.annotation.*;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
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.CorsRegistry;
|
||||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||||
|
|
||||||
|
@ -55,12 +55,12 @@ public class UserServiceApplication {
|
||||||
|
|
||||||
// Authenticate a user
|
// Authenticate a user
|
||||||
@PostMapping("/authenticate")
|
@PostMapping("/authenticate")
|
||||||
public String authenticate(@RequestParam String email, @RequestParam String password) {
|
public AuthResponse authenticate(@RequestBody AuthRequest authRequest) {
|
||||||
return userDatabase.values().stream()
|
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()
|
.findFirst()
|
||||||
.map(user -> "Authentication successful for user ID: " + user.getId())
|
.map(user -> new AuthResponse(user.getId(), "Authentication successful", true))
|
||||||
.orElse("Authentication failed");
|
.orElse(new AuthResponse(null, "Authentication failed", false));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update user details (excluding password)
|
// Update user details (excluding password)
|
||||||
|
@ -90,6 +90,52 @@ public class UserServiceApplication {
|
||||||
return "User deleted successfully";
|
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
|
// User entity
|
||||||
static class User {
|
static class User {
|
||||||
private Long id;
|
private Long id;
|
||||||
|
@ -98,7 +144,6 @@ public class UserServiceApplication {
|
||||||
private String password;
|
private String password;
|
||||||
private String role; // REQUESTER, VOLUNTEER, ADMIN
|
private String role; // REQUESTER, VOLUNTEER, ADMIN
|
||||||
|
|
||||||
// Getters and setters
|
|
||||||
public Long getId() {
|
public Long getId() {
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,61 +4,22 @@
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
<title>HelpApp</title>
|
<title>HelpApp</title>
|
||||||
<style>
|
|
||||||
body {
|
|
||||||
font-family: Arial, sans-serif;
|
|
||||||
margin: 0;
|
|
||||||
padding: 0;
|
|
||||||
background-color: #f9f9f9;
|
|
||||||
}
|
|
||||||
header {
|
|
||||||
background-color: #4CAF50;
|
|
||||||
color: white;
|
|
||||||
text-align: center;
|
|
||||||
padding: 1rem 0;
|
|
||||||
}
|
|
||||||
main {
|
|
||||||
padding: 2rem;
|
|
||||||
}
|
|
||||||
section {
|
|
||||||
margin-bottom: 2rem;
|
|
||||||
padding: 1rem;
|
|
||||||
border: 1px solid #ccc;
|
|
||||||
border-radius: 5px;
|
|
||||||
background-color: white;
|
|
||||||
}
|
|
||||||
section h2 {
|
|
||||||
margin-top: 0;
|
|
||||||
}
|
|
||||||
form {
|
|
||||||
display: flex;
|
|
||||||
flex-direction: column;
|
|
||||||
}
|
|
||||||
form input, form textarea, form select, form button {
|
|
||||||
margin-bottom: 1rem;
|
|
||||||
padding: 0.5rem;
|
|
||||||
font-size: 1rem;
|
|
||||||
}
|
|
||||||
button {
|
|
||||||
background-color: #4CAF50;
|
|
||||||
color: white;
|
|
||||||
border: none;
|
|
||||||
cursor: pointer;
|
|
||||||
}
|
|
||||||
button:hover {
|
|
||||||
background-color: #45a049;
|
|
||||||
}
|
|
||||||
.response {
|
|
||||||
margin-top: 1rem;
|
|
||||||
color: #555;
|
|
||||||
}
|
|
||||||
</style>
|
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<header>
|
<header>
|
||||||
<h1>HelpApp</h1>
|
<h1>HelpApp</h1>
|
||||||
</header>
|
</header>
|
||||||
<main>
|
<main>
|
||||||
|
<section id="user-login">
|
||||||
|
<h2>Connexion</h2>
|
||||||
|
<form id="login-form">
|
||||||
|
<input type="email" id="login-email" placeholder="Email" required>
|
||||||
|
<input type="password" id="login-password" placeholder="Mot de passe" required>
|
||||||
|
<button type="submit">Se connecter</button>
|
||||||
|
</form>
|
||||||
|
<div class="response" id="login-response"></div>
|
||||||
|
</section>
|
||||||
|
|
||||||
<section id="user-registration">
|
<section id="user-registration">
|
||||||
<h2>Créer un compte utilisateur</h2>
|
<h2>Créer un compte utilisateur</h2>
|
||||||
<form id="register-form">
|
<form id="register-form">
|
||||||
|
@ -83,14 +44,69 @@
|
||||||
</form>
|
</form>
|
||||||
<div class="response" id="request-response"></div>
|
<div class="response" id="request-response"></div>
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
<section id="view-requests">
|
|
||||||
<h2>Voir les demandes d'aide</h2>
|
|
||||||
<button id="fetch-requests">Afficher les demandes</button>
|
|
||||||
<ul id="request-list"></ul>
|
|
||||||
</section>
|
|
||||||
</main>
|
</main>
|
||||||
|
|
||||||
<script src="script.js"></script>
|
<script>
|
||||||
|
const API_BASE = "http://localhost";
|
||||||
|
|
||||||
|
document.addEventListener("DOMContentLoaded", () => {
|
||||||
|
const loginForm = document.getElementById("login-form");
|
||||||
|
const loginResponse = document.getElementById("login-response");
|
||||||
|
const registerForm = document.getElementById("register-form");
|
||||||
|
const registerResponse = document.getElementById("register-response");
|
||||||
|
const userRegistrationSection = document.getElementById("user-registration");
|
||||||
|
|
||||||
|
// Check if user is logged in
|
||||||
|
if (localStorage.getItem("userId")) {
|
||||||
|
userRegistrationSection.style.display = "none";
|
||||||
|
}
|
||||||
|
|
||||||
|
// User Login
|
||||||
|
loginForm.addEventListener("submit", async (e) => {
|
||||||
|
e.preventDefault();
|
||||||
|
const email = document.getElementById("login-email").value;
|
||||||
|
const password = document.getElementById("login-password").value;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch(`${API_BASE}:8083/users/authenticate`, {
|
||||||
|
method: "POST",
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
body: JSON.stringify({ email, password }),
|
||||||
|
});
|
||||||
|
const result = await response.json();
|
||||||
|
if (result.success) {
|
||||||
|
localStorage.setItem("userId", result.userId);
|
||||||
|
loginResponse.textContent = `Connexion réussie : ID ${result.userId}`;
|
||||||
|
userRegistrationSection.style.display = "none";
|
||||||
|
} else {
|
||||||
|
loginResponse.textContent = `Échec de la connexion : ${result.message}`;
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
loginResponse.textContent = "Erreur lors de la connexion.";
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// User Registration
|
||||||
|
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.";
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
|
@ -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 = "<li>Erreur lors de la récupération des demandes.</li>";
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
Loading…
Add table
Reference in a new issue