java.lang.Object
ai.tutor.cab302exceptionalhandlers.controller.AuthController
Direct Known Subclasses:
LoginController, SignUpController

public abstract class AuthController extends Object
Abstract base class for authentication controllers in the AI tutor application.

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 Details

    • db

      protected final SQLiteConnection db
    • userDAO

      protected final UserDAO 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

      public AuthController(SQLiteConnection db) throws SQLException, RuntimeException
      Constructs an AuthController with a database connection.

      Initializes the db connection and creates a UserDAO instance for user database operations.

      Parameters:
      db - The SQLite database connection
      Throws:
      SQLException - If database initialization fails
      RuntimeException - 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, or confirmPasswordField 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

      protected void errorFeedback(javafx.scene.control.Label feedbackLabel, String message)
      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 message
      message - The error message to show
    • resetErrorFeedback

      protected void resetErrorFeedback()
      Clears error feedback from all feedback labels.

      Resets the text of usernameFeedback and passwordFeedback 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 authenticated User to load the chat for
      Throws:
      IllegalStateException - If the user is not specified
      RuntimeException - If database connection fails
      SQLException - If database operations fail
      IOException - If loading the chat screen fails
    • switchLayout

      protected void switchLayout() throws Exception
      Switches between login and sign-up screens.

      Determines the target screen based on the current controller type (e.g., from LoginController to SignUpController or vice versa) and uses SceneManager to navigate to the appropriate authentication screen.

      Throws:
      IOException - If loading the target screen fails
      SQLException - If database operations fail
      RuntimeException - If unexpected errors occur
      Exception
    • 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 calls switchLayout() 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 calls onSubmit() 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 and passwordField that call onFieldChanged(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 authenticate
      password - The password to validate or hash
      Returns:
      The authenticated or newly created User object
      Throws:
      IllegalArgumentException - If the username or password is invalid
      SecurityException - If authentication fails (e.g., incorrect password)
      SQLException - If database operations fail