The authentication utility simplifies the process of registering and login users into the database, meaning that you only have to worry about giving it the user data to register and/or login.
It's built on top of the
\Core\DB class so it uses PDO.
Just remember to
Register a new user into the database.
This method takes as parameter an array which will be the user data to insert. The only required keys that the array must have are
password_confirm, both values must be a string and equal.
This method returns true if the user has been successfully inserted into the database, false otherwise.
$user = [ 'name' => 'Alejandro', 'email' => '[email protected]', 'password' => 'canislupus', 'password_confirm' => 'canislupus', ]; Auth::register($user);
Take in consideration the following points:
The password is hashed before storing it, using the
BCRYPT algorithm with a default cost of 10.
The array keys are directly maped to the database table (except for the
password_confirm key). Meaning that an array with the following keys:
phone, will be inserted into a table that must have a
phone columns for this to work.
Returns true if the given user data exists in the database and is valid, false otherwise.
This method takes as parameter an array which will be the user data to validate. The only required key that the array must have is
$user = [ 'email' => '[email protected]', 'password' => 'canislupus' ]; Auth::login($user);
If a user with the giving email and password exists in the
user table (in this example), it will return true.
Set the name of the database table that will be used to register and login users. By default its value is
Get the name of the database table that will be used to register and login users.
Set the options that will be used when hashing passwords. The parameter must be an associative array.
This is the equivalent to defining the third parameter of the
password_hash function that is used inside the code of this class.
$options = [ 'cost' => 16 ]; Auth::setOptions($options);
That will set the cost of the pasword hashing function to 16. By default it's 10.
Get the options that will be used when hashing passwords.
Get the id of the last inserted/registered user into the database.
Get the currently authenticated user data.
This function returns an associative array with the user data.
If no user has been logged in previously with the
login method or if the last login attempt failed this will return