VELOX API Docs

Auth
in package

A class that serves as an authentication system for users.

Example:

// register a new user
$auth = new Auth(); // or Auth::instance();
$status = $auth->register('username', 'password');

// unregister a user
$status = Auth::instance()->unregister('username');

// log in a user
$status = Auth::instance()->login('username', 'password');

// log out a user
Auth::instance()->logout();

// authenticate a user model
Auth::authenticate($user);

// check if there is a logged in user
$status = Auth::check();

// retrieve the current authenticated user
$user = Auth::user();

// add HTTP basic auth
Auth::basic(['username' => 'password']);
Tags
since
1.4.0

Table of Contents

AFTER_REGISTER  = 'auth.after.register'
This event will be dispatched after an auth user is registered.
ON_LOGIN  = 'auth.on.login'
This event will be dispatched when an auth user is logged in.
ON_LOGOUT  = 'auth.on.logout'
This event will be dispatched when an auth user is logged out.
ON_REGISTER  = 'auth.on.register'
This event will be dispatched when an auth user is registered.
ON_UNREGISTER  = 'auth.on.unregister'
This event will be dispatched when an auth user is unregistered.
$instance  : self
The class singleton instance.
$user  : Model
Auth user model.
__construct()  : mixed
Class constructor.
authenticate()  : void
Authenticates an auth user model.
basic()  : void
Serves as an HTTP Basic Authentication guard for the specified logins.
check()  : bool
Checks if a user is logged in and logs the user out if the timeout has expired.
fail()  : void
Renders 401 error page.
instance()  : static
Returns the singleton instance of the class.
login()  : bool
Logs in a user.
logout()  : void
Logs out a user.
register()  : bool
Registers a new user.
unregister()  : bool
Unregisters a user.
user()  : Model|null
Returns the authenticated user model instance.
getUserModel()  : Model
Returns an instance of the user model class specified in the config or falls back to the default one.
hash()  : string
Hashes a password.
verify()  : bool
Verifies a password.

Constants

AFTER_REGISTER

This event will be dispatched after an auth user is registered.

public string AFTER_REGISTER = 'auth.after.register'

This event will be passed the user model object and its listener callback will be bound to the object (the auth class instance).

ON_LOGIN

This event will be dispatched when an auth user is logged in.

public string ON_LOGIN = 'auth.on.login'

This event will be passed the user model object and its listener callback will be bound to the object (the auth class instance).

ON_LOGOUT

This event will be dispatched when an auth user is logged out.

public string ON_LOGOUT = 'auth.on.logout'

This event will be passed the user model object and its listener callback will be bound to the object (the auth class instance).

ON_REGISTER

This event will be dispatched when an auth user is registered.

public string ON_REGISTER = 'auth.on.register'

This event will be passed the user model object and its listener callback will be bound to the object (the auth class). This event is useful if the user model class has additional attributes other than the username and password that need to be set.

ON_UNREGISTER

This event will be dispatched when an auth user is unregistered.

public string ON_UNREGISTER = 'auth.on.unregister'

This event will be passed the user model object and its listener callback will be bound to the object (the auth class instance).

Properties

$instance

The class singleton instance.

protected static self $instance

Methods

__construct()

Class constructor.

public __construct([string $model = null ]) : mixed
Parameters
$model : string = null

[optional] The auth user model class to use.

Return values
mixed

authenticate()

Authenticates an auth user model.

public static authenticate(Model $user) : void
Parameters
$user : Model

The auth user model to authenticate.

Tags
throws
DomainException

If the user could not be authenticated or the model is not an auth user model.

Return values
void

basic()

Serves as an HTTP Basic Authentication guard for the specified logins.

public static basic(array<string|int, mixed> $logins) : void
Parameters
$logins : array<string|int, mixed>

The login data, an associative array where key is the username and value is the password.

Tags
throws
InvalidArgumentException

If no logins where provided.

codeCoverageIgnore

Can't test methods that send headers.

Return values
void

check()

Checks if a user is logged in and logs the user out if the timeout has expired.

public static check() : bool
Return values
bool

fail()

Renders 401 error page.

public static fail() : void
Tags
codeCoverageIgnore

Can't test methods that send headers.

Return values
void

instance()

Returns the singleton instance of the class.

public final static instance() : static

NOTE: This method returns only the first instance of the class which is normally the one that was created during application bootstrap.

Return values
static

login()

Logs in a user.

public login(string $username, string $password) : bool
Parameters
$username : string

Auth user username.

$password : string

Auth user password.

Return values
bool

True if the user was logged in successfully, false if the user is not registered or the password is incorrect.

logout()

Logs out a user.

public logout() : void
Return values
void

register()

Registers a new user.

public register(string $username, string $password) : bool
Parameters
$username : string

Auth user username.

$password : string

Auth user password.

Return values
bool

True if the user was registered successfully, false if the user is already registered.

unregister()

Unregisters a user.

public unregister(string $username) : bool
Parameters
$username : string

Auth user username.

Return values
bool

True if the user was unregistered successfully, false if the user is not registered.

user()

Returns the authenticated user model instance.

public static user() : Model|null
Return values
Model|null

The authenticated user or null if no user has logged in.

getUserModel()

Returns an instance of the user model class specified in the config or falls back to the default one.

protected static getUserModel([string $model = null ]) : Model
Parameters
$model : string = null

[optional] The auth user model class to use.

Return values
Model

hash()

Hashes a password.

protected hash(string $password) : string
Parameters
$password : string
Return values
string

The hashed password.

verify()

Verifies a password.

protected verify(string $password, string $hash) : bool
Parameters
$password : string
$hash : string
Return values
bool

Search results