Skip to content

Commit

Permalink
fix&update[UserService]: change login to return a User, easier connec…
Browse files Browse the repository at this point in the history
…tion to frontend
  • Loading branch information
Fredrik Marjoni committed Mar 19, 2026
1 parent b356fbe commit 28eb0a1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 21 deletions.
13 changes: 8 additions & 5 deletions src/main/java/edu/group5/app/model/user/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
36 changes: 20 additions & 16 deletions src/test/java/edu/group5/app/model/user/UserServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand All @@ -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);
}
}

0 comments on commit 28eb0a1

Please sign in to comment.