The Login Module
The Login module provides portable, configurable authentication for one or more user levels. Each user level maps to a target database table, with configurable login identifiers (username, email, etc.), redirect destinations, and view styles. Features include forgot-password reset flows, brute-force protection through rate limiting, CSRF-protected forms, and remember-me cookie support.
__construct()
Constructor for the Login module. Calls the parent constructor and prevents direct URL access to the module.
credentials_valid()
Validation callback used during login form submission. Checks whether the submitted identifier (username or email) corresponds to an existing, non-blocked user account in the configured target table.
forgot_password()
Displays the forgot-password form where users can enter their email address to request a password reset link. A valid email identifier must be configured in config/login.php for the user's level.
hash_password()
Hashes a plain-text password using bcrypt with the configured cost factor from config/login.php. Uses PHP's built-in password_hash() function with the PASSWORD_DEFAULT algorithm.
index()
Default method for the Login module. Redirects users to the homepage if they are already logged in, or displays the login form for the configured user level.
is_logged_in()
Checks whether the current user has a valid authentication token. Optionally filters by a specific user level. Useful for conditional logic in controllers where you need to check authentication status without triggering a redirect.
login()
Displays the login form for the requested user level. The user level is determined from the URL segment using either a numeric level ID or a configured secret login word. If the user already has a valid token, they are redirected to their configured success page.
logout()
Logs out the current user by destroying their authentication token and clearing session and cookie data. Redirects to the login form for the user's level (determined by the active token), using the configured secret login word or numeric level ID. Falls back to the homepage only when no active session is found.
not_allowed()
Displays an access denied page for users who have exceeded the maximum number of allowed login attempts. The block duration is configured in config/login.php via the block_duration setting (default: 900 seconds).
reset_password()
Displays the password reset form. The form is accessed via a time-sensitive link that contains a reset token. If the token is invalid or has expired, an error message is displayed.
show_404()
Displays a 404 error page. Called when the login module cannot determine a valid user level from the URL, for example when an invalid secret login word is provided.
submit_forgot_password()
Processes the forgot-password form submission. Looks up the user by email address, generates a time-sensitive reset token, and sends a password reset email via the trongate_email module. The reset link expiry is controlled by reset_token_lifespan in config/login.php.
submit_login()
Handles login form submission. Validates credentials against the configured target table, checks rate limiting, runs validation rules, and either logs the user in (generating a token) or records the failed attempt and redisplayes the form with errors.
submit_reset_password()
Processes the password reset form submission. Validates the reset token, updates the user's password in the database, destroys all existing authentication tokens for that user (forcing re-login on all devices), and redirects to the login page.
unlock()
Unlocks a previously blocked account after the block duration has expired. This method checks whether the rate-limit period has elapsed and, if so, clears the failed attempt counter and redirects the user back to the login form.