-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: Added AuthenticationService object and added new method to chec…
…k if username is taken
- Loading branch information
AdrianBalunan
committed
Apr 15, 2026
1 parent
939ef16
commit 303b852
Showing
3 changed files
with
140 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
...lpapplication/src/main/java/ntnu/systemutvikling/team6/service/AuthenticationService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } |