From 303b85274671d8d35258fb919f20c833d8057a28 Mon Sep 17 00:00:00 2001 From: AdrianBalunan Date: Wed, 15 Apr 2026 10:31:02 +0200 Subject: [PATCH] Feat: Added AuthenticationService object and added new method to check if username is taken --- .../team6/database/DatabaseSetup.java | 1 - .../team6/database/Readers/UserSelect.java | 24 ++++ .../team6/service/AuthenticationService.java | 116 ++++++++++++++++++ 3 files changed, 140 insertions(+), 1 deletion(-) diff --git a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/database/DatabaseSetup.java b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/database/DatabaseSetup.java index e5775da..e809dd4 100644 --- a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/database/DatabaseSetup.java +++ b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/database/DatabaseSetup.java @@ -42,7 +42,6 @@ public boolean testConnection() { System.out.println("Database connection failed."); e.printStackTrace(); } - return false; } diff --git a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/database/Readers/UserSelect.java b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/database/Readers/UserSelect.java index 2394c06..4f46745 100644 --- a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/database/Readers/UserSelect.java +++ b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/database/Readers/UserSelect.java @@ -30,6 +30,30 @@ public UserSelect(DatabaseConnection connection) { this.connection = connection; } + public boolean isUsernameTaken(String username){ + try (Connection conn = connection.getMySqlConnection(); + Statement stmt = conn.createStatement()) { + + String mysql = + """ + SELECT UUID_User FROM User WHERE user_name = ? + """; + PreparedStatement statement = conn.prepareStatement(mysql); + statement.setString(1, username); + ResultSet rs = statement.executeQuery(); + + if (rs.next()) { + System.out.println("Username Taken already"); + return true; + } + + } catch (SQLException e) { + e.printStackTrace(); + } + return false; + } + + /** * Retrieves a single {@link User} from the database matching the given username and password. * diff --git a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/service/AuthenticationService.java b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/service/AuthenticationService.java index 8b13789..ee78058 100644 --- a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/service/AuthenticationService.java +++ b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/service/AuthenticationService.java @@ -1 +1,117 @@ +package ntnu.systemutvikling.team6.service; +import ntnu.systemutvikling.team6.database.DAO.UserDAO; +import ntnu.systemutvikling.team6.database.Readers.UserSelect; +import ntnu.systemutvikling.team6.models.user.Inbox; +import ntnu.systemutvikling.team6.models.user.Role; +import ntnu.systemutvikling.team6.models.user.Settings; +import ntnu.systemutvikling.team6.models.user.User; + + +/** + * Service class responsible for handling user authentication operations, + * including login, registration, and logout functionality. + *

+ * Maintains the state of the currently authenticated user throughout the session. + *

+ */ +public class AuthenticationService { + /** Handles read operations for user data from the database. */ + private final UserSelect userDataReader; + /** Handles write operations for user data to the database. */ + private final UserDAO userDataSender; + + /** The currently authenticated user, or {@code null} if no user is logged in. */ + private User currentUser; + + /** + * Constructs an {@code AuthenticationService} with the specified data access objects. + * + * @param userDataReader the data reader used to query user information from the database + * @param userDataSender the DAO used to persist new user registrations to the database + */ + public AuthenticationService(UserSelect userDataReader, UserDAO userDataSender) { + this.userDataReader = userDataReader; + this.userDataSender = userDataSender; + } + + /** + * Attempts to authenticate a user with the given credentials. + *

+ * If a matching user is found in the database, they are set as the current user + * and the method returns {@code true}. + *

+ * + * @param username the username of the user attempting to log in + * @param password the password of the user attempting to log in + * @return {@code true} if authentication was successful; {@code false} otherwise + */ + public boolean login(String username, String password){ + User user = userDataReader.getUserFromDBUsernameAndPassword(username, password); + + if (user != null){ + currentUser = user; + return true; + } + + return false; + } + + /** + * Registers a new user account with the provided details. + *

+ * The new user is assigned the {@link Role#NORMAL_USER} role and default + * {@link Settings} and {@link Inbox}. Registration will fail if the + * username is already taken or if the database operation is unsuccessful. + * On success, the new user is set as the current user. + *

+ * + * @param displayName the display name shown on the user's profile + * @param username the unique username for the new account + * @param email the email address associated with the new account + * @param password the password for the new account + * @return {@code true} if registration was successful; {@code false} if the + * username is already taken or the database operation failed + */ + public boolean register(String displayName, String username, String email, String password ){ + User newUser = new User(displayName, username, email, password, Role.NORMAL_USER, new Settings(), new Inbox()); + + if(userDataReader.isUsernameTaken(username)){ + return false; + } + + boolean success = userDataSender.registerUser(newUser); + + if (success){ + currentUser = newUser; + return true; + } + return false; + } + + /** + * Logs out the currently authenticated user by clearing the current user state. + */ + public void logout (){ + currentUser = null; + } + + + /** + * Returns the currently authenticated user. + * + * @return the current {@link User}, or {@code null} if no user is logged in + */ + public User getCurrentUser(){ + return currentUser; + } + + /** + * Checks whether a user is currently logged in. + * + * @return {@code true} if a user is authenticated; {@code false} otherwise + */ + public boolean isLoggedin(){ + return currentUser != null; + } +}