Skip to content

Commit

Permalink
Feat: Added AuthenticationService object and added new method to chec…
Browse files Browse the repository at this point in the history
…k if username is taken
  • Loading branch information
AdrianBalunan committed Apr 15, 2026
1 parent 939ef16 commit 303b852
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public boolean testConnection() {
System.out.println("Database connection failed.");
e.printStackTrace();
}

return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
* <p>
* Maintains the state of the currently authenticated user throughout the session.
* </p>
*/
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.
* <p>
* If a matching user is found in the database, they are set as the current user
* and the method returns {@code true}.
* </p>
*
* @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.
* <p>
* 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.
* </p>
*
* @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;
}
}

0 comments on commit 303b852

Please sign in to comment.