Table of Contents
Authentication is the act of confirming the identity of an individual. Although it can have many uses, you will mainly need to use it to identify the users of your application when they log in or perform an action requiring the confirmation of their identity (like accessing the administration panel of your application).
Authentication usually requires the individual to communicate its identifier and an associated password. The identifier can be anything, ranging from a pseudonym, an email address, or an ID number. An authentication request is only valid if the identifier and the password matches the ones stored in the authentication server.
Note that the authentication mechanisms expects identifiers to be unique in the authentication server.
The database table authentication driver allows you to authenticate users against fields in a table from a database. This is especially useful if you have a table containing information, including credentials, for all users of your application.
Assuming you want to authenticate against a table named users containing the identifier field
user_email and the password field user_password, you can use the following code:
<?php // This could be data sent by a form, for example $aTest = array( 'identifier' => 'test@example.org', 'password' => 'mypassword42', ); // Create the authentication driver $oAuth = new weeAuthDbTable(array( 'db' => $oDb, // the database driver 'table' => 'users', 'identifier_field' => 'user_email', 'password_field' => 'user_password', )); try { $aResult = $oAuth->authenticate($aTest); // Authentication succeeded } catch (AuthenticationException $e) { // Authentication failed }
As you can see, an AuthenticationException is thrown when authentication fails.
This allows you to easily detect it and act upon it if you need it.
And if you don't need to, you don't even have to catch it.
When the authentication succeeds, an array is returned that contains
all the information returned by the authentication server about the user.
This effectively equals a row of the users table, in our example.
The default treatment applied to the password is sha1.
This can be changed by specifying the password_treatment parameter to the constructor.
This allows you to store the password in other forms as needed.