Milk Admin

Auth Class Documentation

This documentation provides a detailed overview of the Auth class and its functions.

Settings

In config.php you can set the session duration in minutes before it expires.

$conf['auth_expires_session'] = 20;

You can also set the "remember me" duration in days.

$conf['auth_remember_me_duration'] = 30;

Introduction

The Auth class handles authentication and users within the system. It allows login, logout, session management and user permissions.

Variables

  • $current_user: Currently authenticated user.
  • $expired_session: Session duration before it expires.
  • $session: Current session data.

Functions

getInstance()

Returns the singleton instance of the Auth class.


$auth = Get::make('Auth');
        

getUser($id = 0)

Returns the current user or a specific user if ID is set.
After the 'modules_loaded' hook, the current user is set to guest or to the user who logged in


$user = $auth->getUser(1);
        

The user has the following data:

  • id: User ID.
  • username: Username.
  • email: User email.
  • password: User password.
  • status: User status. 1:Active, 0 Disabled, -1 Deleted
  • is_admin: Administrator user flag.
  • is_guest: Guest user flag.
  • permissions: User permissions.

Permissions are not used directly by the Auth class but are transferred to the Permissions class.

if (Permissions::check('_user.is_guest')) {
                // the user is NOT logged in
            } 

Outside the module you can access the Auth class as follows:


// ...
$user = Get::make('Auth')->getUser();
$user->is_guest;
$user->is_admin;

login($username_email = '', $password = '')

Verifies if the credentials are correct or if the user is already logged in.


$is_authenticated_in = $auth->login('username', 'password');
        

isAuthenticated()

Checks if the user is logged in.


$is_authenticated = $auth->isAuthenticated();
        

logout()

Logs out the current user.


$auth->logout();
        

saveUser($id, $username, $email, $password = '', $status = 1, $is_admin = 0, $permissions = [])

Saves a user to the database.


$auth->saveUser(1, 'username', 'email@example.com', 'password', 1, 0, []);
        

Deep Dive

When login is performed:

First of all, you need to understand that authentication management is executed within the normal execution of the site modules when Get::make('Auth'); is called. Inside auth.module.php the modules_loaded hook is executed which initializes the user.


Hooks::set('modules_loaded', function() {
    Get::make('Auth');
});

Hooks allow you to manage the execution order. By default the order is set to 20, while auth is set to 10. This way authentication is set before all other modules.

Print current user:


$user = Get::make('Auth')->getUser();
echo "<pre>"; print_r($user); echo "</pre>";
// Output:
stdClass Object
(
    [id] => 0
    [username] => Guest
    [email] => 
    [password] => 
    [registered] => 2026-07-17 11:40:22
    [status] => 1
    [is_admin] => 0
    [permissions] => Array
        (
            [_user] => Array
                (
                    [is_admin] => 
                    [is_guest] => 1
                )

        )

    [is_guest] => 1
)
Loading...