Auth package

The Auth package provides a standardized interface for authentication in Fuel. This allows our users to write their own drivers and easily integrate a new driver to work with old code by keeping the basic methods consistent.

Interface

The Auth class provides a static interface for generic methods, and contains a __callStatic magic method which allows you to fetch loaded driver instances, and call driver methods statically (only if multiple concurrent drivers are disabled in the auth config!).

forge($custom = array())

Instantiates the Auth driver or drivers defined in the Auth configuration file. Optionally, you can pass a custom configuration array to define the driver that needs to be instantiated.

Static Yes
Parameters
Param Default Description
$custom mixed Driver configuration array, or string containing the driver name to instantiate. If not given, the values from the Auth config file are used.
Returns Auth_Login_Driver instance
Throws AuthException, if the defined driver failed to load, or if another drivers is already instantiated using the same driver id.
Example
// Create the default instance(s)
Auth::forge();

// Create the SimpleAuth instance
Auth::forge('simpleauth');

// or specify multiple driver settings
Auth::forge(array('driver' => 'simpleauth', 'id' => 'customid'));

If you only use a single Auth driver configured in the Auth configuration file, you don't need to call this method manually. The default instance will be created automatically the first time the default instance is requested by one of the other methods.

instance($instance = null)

Retrieves a loaded driver instance. When multiple drivers are set in config the first driver loaded will be set as the default driver.

Static Yes
Parameters
Param Default Description
$instance null null to fetch the default driver, or a driver id to get a specific one
Returns Auth_Login_Driver
Example
// Logout the default driver
Auth::instance()->logout();

// Login the SimpleAuth driver
Auth::instance('simpleauth')->login(Input::post('username'), Input::post('password'));

If the default instance is requested, but none is loaded, instance() will call forge() to create the default instance based on the settings defined in the Auth configuration file.

unload($driver_id = null)

Removes a loaded driver instance. If null, the default driver instance will be removed.

Static Yes
Parameters
Param Default Description
$driver_id null null to remove the default driver, or a driver id to remove a specific one
Returns boolean, true if the unload succeeded, false otherwise.
Throws AuthException, when $driver_id is not a valid driver.
Example
// Remove the default driver
Auth::unload();

// Remove the SimpleAuth driver
Auth::unload('simpleauth');

check($specific = null)

Checks either all loaded Login drivers or just those specified as the parameter.

Static Yes
Parameters
Param Default Description
$specific null null to check all or one ore more driver id's (single string or array of strings)
Returns bool, whether one of the (given) drivers successfully validated the login
Example
if ( ! Auth::check())
{
	Response::redirect('login');
}

// specific driver
Auth::check('simpleauth');

// Multiple drivers
Auth::check(array('simpleauth', 'twitter'));

verified($specific = null)

Get a specific verified driver or all verified drivers.

Static Yes
Parameters
Param Default Description
$specific null null to return all verified drivers in an array, or one specific driver id
Returns Array|Auth_Login_Driver|false. When
Example
// check if the simpleauth driver is verified
$driver = Auth::verified('simpleauth');

logout()

Logout of all currently logged in drivers.

register_driver_type($type, $check_method)

Registers a new driver type.

Static Yes
Parameters
Param Default Description
$type required Type of Auth driver to be registered.
$check_method required The driver method to be called when verifying drivers of this type.
Example
// register the 'role' driver type
if ( ! Auth::register_driver_type('role', 'check_role'))
{
	throw new AuthException('role could not be registered');
}

Both type and method must be unique amongst all registered drivers. This method returns 'false' if this is not the case.

unregister_driver_type($type)

Unregisters a driver type.

Static Yes
Parameters
Param Default Description
$type required Type of Auth driver to be removed.
Example
// remove the 'role' driver type
Auth::unregister_driver_type('role');

Note that the default drivers types, 'login', 'group' and 'acl' can not be unregistered!