Class AuthController
- Direct Known Subclasses:
LoginController
,SignUpController
Provides common functionality for handling login and sign-up forms, including
input validation, UI feedback, and navigation. Subclasses (LoginController
,
SignUpController
) implement specific authentication logic for logging in
or creating users. Interacts with the database via UserDAO
and manages
scene transitions using SceneManager
.
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected javafx.scene.control.PasswordField
protected final SQLiteConnection
protected boolean
protected javafx.scene.control.Label
protected javafx.scene.control.PasswordField
protected javafx.scene.control.Button
protected javafx.scene.control.Button
protected final UserDAO
protected boolean
protected javafx.scene.control.Label
protected javafx.scene.control.TextField
-
Constructor Summary
ConstructorsConstructorDescriptionConstructs an AuthController with a database connection. -
Method Summary
Modifier and TypeMethodDescriptionabstract User
authenticateUser
(String username, String password) Authenticates a user with the provided username and password.protected void
errorFeedback
(javafx.scene.control.Label feedbackLabel, String message) Displays error feedback in the specified label.void
Initializes the authentication form’s UI components and event handlers.void
Navigates to the chat screen for the authenticated user.protected abstract void
onFieldChanged
(javafx.scene.input.KeyEvent e) Handles key events in the authentication form’s input fields.protected abstract void
onSubmit()
Processes the authentication form submission.protected void
Clears error feedback from all feedback labels.protected void
Configures input fields to respond to key events.protected void
Configures the submit button to handle form submission.protected void
Configures the switch layout button to handle navigation between login and sign-up screens.protected abstract void
Enables or disables the submit button based on input field states.protected void
Switches between login and sign-up screens.
-
Field Details
-
db
-
userDAO
-
usernameEmpty
protected boolean usernameEmpty -
passwordEmpty
protected boolean passwordEmpty -
usernameField
protected javafx.scene.control.TextField usernameField -
passwordField
protected javafx.scene.control.PasswordField passwordField -
confirmPasswordField
protected javafx.scene.control.PasswordField confirmPasswordField -
submitButton
protected javafx.scene.control.Button submitButton -
usernameFeedback
protected javafx.scene.control.Label usernameFeedback -
passwordFeedback
protected javafx.scene.control.Label passwordFeedback -
switchLayout
protected javafx.scene.control.Button switchLayout
-
-
Constructor Details
-
AuthController
Constructs an AuthController with a database connection.Initializes the
db
connection and creates aUserDAO
instance for user database operations.- Parameters:
db
- The SQLite database connection- Throws:
SQLException
- If database initialization failsRuntimeException
- If unexpected errors occur during setup
-
-
Method Details
-
initialize
public void initialize()Initializes the authentication form’s UI components and event handlers.Sets up the switch layout button (
setupSwitchLayoutButton()
), submit button (setupSubmitButton()
), and input fields (setupInputField()
) for user interaction. Called automatically by JavaFX when the FXML is loaded. -
onFieldChanged
protected abstract void onFieldChanged(javafx.scene.input.KeyEvent e) Handles key events in the authentication form’s input fields.Abstract method to be implemented by subclasses to track changes in
usernameField
,passwordField
, orconfirmPasswordField
and update the submit button’s state.- Parameters:
e
- The key event triggered by typing in an input field
-
submitButtonToggle
protected abstract void submitButtonToggle()Enables or disables the submit button based on input field states.Abstract method to be implemented by subclasses to control the
submitButton
based on whether required fields are filled. -
errorFeedback
Displays error feedback in the specified label.Sets the text of the given feedback label (e.g.,
usernameFeedback
,passwordFeedback
) to the provided error message.- Parameters:
feedbackLabel
- The label to display the error messagemessage
- The error message to show
-
resetErrorFeedback
protected void resetErrorFeedback()Clears error feedback from all feedback labels.Resets the text of
usernameFeedback
andpasswordFeedback
to empty strings, clearing any previous error messages. -
onSubmit
protected abstract void onSubmit()Processes the authentication form submission.Abstract method to be implemented by subclasses to handle form submission, such as validating input and authenticating the user.
-
loadChat
public void loadChat(User user) throws IllegalStateException, RuntimeException, SQLException, IOException Navigates to the chat screen for the authenticated user.Uses
SceneManager
to load the chat interface for the specified user.- Parameters:
user
- The authenticatedUser
to load the chat for- Throws:
IllegalStateException
- If the user is not specifiedRuntimeException
- If database connection failsSQLException
- If database operations failIOException
- If loading the chat screen fails
-
switchLayout
Switches between login and sign-up screens.Determines the target screen based on the current controller type (e.g., from
LoginController
toSignUpController
or vice versa) and usesSceneManager
to navigate to the appropriate authentication screen.- Throws:
IOException
- If loading the target screen failsSQLException
- If database operations failRuntimeException
- If unexpected errors occurException
-
setupSwitchLayoutButton
protected void setupSwitchLayoutButton()Configures the switch layout button to handle navigation between login and sign-up screens.Attaches an event handler to
switchLayout
that callsswitchLayout()
when clicked, with error handling to display alerts for failures. -
setupSubmitButton
protected void setupSubmitButton()Configures the submit button to handle form submission.Attaches an event handler to
submitButton
that callsonSubmit()
when clicked, with error handling to display alerts for failures. -
setupInputField
protected void setupInputField()Configures input fields to respond to key events.Attaches event handlers to
usernameField
andpasswordField
that callonFieldChanged(KeyEvent)
when the user types, enabling real-time input validation. -
authenticateUser
public abstract User authenticateUser(String username, String password) throws IllegalArgumentException, SecurityException, SQLException Authenticates a user with the provided username and password.Abstract method to be implemented by subclasses to validate credentials (for login) or create a new user (for sign-up) using
UserDAO
.- Parameters:
username
- The username to authenticatepassword
- The password to validate or hash- Returns:
- The authenticated or newly created
User
object - Throws:
IllegalArgumentException
- If the username or password is invalidSecurityException
- If authentication fails (e.g., incorrect password)SQLException
- If database operations fail
-