From 28eb0a1707ebb742d6d5b391427d1d969cecb8ab Mon Sep 17 00:00:00 2001 From: Fredrik Marjoni Date: Thu, 19 Mar 2026 09:49:53 +0100 Subject: [PATCH] fix&update[UserService]: change login to return a User, easier connection to frontend --- .../group5/app/model/user/UserService.java | 13 ++++--- .../app/model/user/UserServiceTest.java | 36 ++++++++++--------- 2 files changed, 28 insertions(+), 21 deletions(-) diff --git a/src/main/java/edu/group5/app/model/user/UserService.java b/src/main/java/edu/group5/app/model/user/UserService.java index 5df32ee..44e092b 100644 --- a/src/main/java/edu/group5/app/model/user/UserService.java +++ b/src/main/java/edu/group5/app/model/user/UserService.java @@ -65,15 +65,18 @@ public boolean registerUser(String role, String firstName, String lastName, * Authenticates a user based on the provided email and password. * @param email the email address of the user attempting to log in; must not be null or empty * @param password the plaintext password of the user attempting to log in; must not be null or empty - * @return true if the login is successful - * (i.e., the user exists and the password is correct), false otherwise + * @return the authenticated User object if the login is successful + * (i.e., the user exists and the password is correct), null otherwise * @throws IllegalArgumentException if email is null or empty, or if password is null or empty */ - public boolean login(String email, char[] password) { + public User login(String email, char[] password) { if (email == null || email.trim().isEmpty() || password == null || password.length == 0) { - return false; + return null; } User user = this.userRepository.findUserByEmail(email); - return user != null && user.verifyPassword(password); + if (user != null && user.verifyPassword(password)) { + return user; + } + return null; } } diff --git a/src/test/java/edu/group5/app/model/user/UserServiceTest.java b/src/test/java/edu/group5/app/model/user/UserServiceTest.java index 46d63c5..34dbac8 100644 --- a/src/test/java/edu/group5/app/model/user/UserServiceTest.java +++ b/src/test/java/edu/group5/app/model/user/UserServiceTest.java @@ -4,6 +4,8 @@ import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; @@ -95,8 +97,10 @@ void loginValidPassword() { User testUser = new Customer(10, "Test", "User", "test@example.com", hashedPassword); repo.addContent(testUser); - boolean result = service.login("test@example.com", plainPassword.toCharArray()); - assertTrue(result); + User result = service.login("test@example.com", plainPassword.toCharArray()); + assertNotNull(result); + assertEquals("test@example.com", result.getEmail()); + assertTrue(result.verifyPassword(plainPassword.toCharArray())); } @Test @@ -107,23 +111,23 @@ void loginInvalidPassword() { User testUser = new Customer(10, "Test", "User", "test@example.com", hashedPassword); repo.addContent(testUser); - boolean result = service.login("test@example.com", "wrongpass".toCharArray()); - boolean result2 = service.login("test@example.com", null); - boolean result3 = service.login("test@example.com", " ".toCharArray()); - boolean result4 = service.login("test@example.com", new char[0]); - assertFalse(result); - assertFalse(result2); - assertFalse(result3); - assertFalse(result4); + User result = service.login("test@example.com", "wrongpass".toCharArray()); + User result2 = service.login("test@example.com", null); + User result3 = service.login("test@example.com", " ".toCharArray()); + User result4 = service.login("test@example.com", new char[0]); + assertNull(result); + assertNull(result2); + assertNull(result3); + assertNull(result4); } @Test void loginInvalidEmail() { - boolean result = service.login("nonexist@example.com", "password".toCharArray()); - boolean result2 = service.login(null, "password".toCharArray()); - boolean result3 = service.login(" ", "password".toCharArray()); - assertFalse(result); - assertFalse(result2); - assertFalse(result3); + User result = service.login("nonexist@example.com", "password".toCharArray()); + User result2 = service.login(null, "password".toCharArray()); + User result3 = service.login(" ", "password".toCharArray()); + assertNull(result); + assertNull(result2); + assertNull(result3); } } \ No newline at end of file