# Authentication

Wolff\Utils\Auth

The authentication utility simplifies the process of registering and login users into the database.

It's built on top of the Wolff\Core\DB class so it uses PDO.

Starting

First you need to instantiate the Auth class, its constructor looks like this:

__construct([array $data = null[, array $options = null ]])

It takes two parameters, an array with the destination database credentials, and an array which will be used as the options for the password_hash function that the utility uses internally.

If no data array is passed, it will use the credentials defined in the system/config.php file.

$credentials = [
    'dbms'     => 'mysql',
    'server'   => 'localhost',
    'name'     => 'wolff',
    'username' => 'root',
    'password' => '12345'
];

$options = [
    'cost' => 16
];

$auth = new \Wolff\Utils\Auth($credentials, $options);

General methods

Register

register(array $data)

Register a new user into the database.

The only required keys that the given array must have are password and 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: name, email, password and phone, will be inserted into a table that must have a name, email, password and phone columns for this to work.

Login

login(array $data)

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 password.

$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 table

setTable([string $table])

Sets the name of the database table that will be used to register and login users. By default its value is user.

$auth->setTable('admin');

Set unique column

setUnique(string $unique_column)

Sets the name of the unique column that cannot be repeated when registering new users in the table. This function is available to avoid any duplicate entry.

$auth->setUnique('email');

Set options

setOptions(array $options)

Set the options that will be used when hashing passwords.

This is the equivalent to defining the third parameter of the password_hash function that is used internally in this utility.

$options = [
    'cost' => 16
];

$auth->setOptions($options);

That will set the cost of the pasword hashing function to 16. By default it's 10.

Get options

getOptions()

Get the options that will be used when hashing passwords.

$auth->getOptions();

Get last inserted Id

getId()

Returns the id of the last inserted/registered user into the database.

$auth->getId();

Get current user

getUser()

Returns the currently authenticated user data.

This function returns an associative array with the user data.

$auth->getUser();

If no user has been logged in previously with the login method or if the last login attempt failed this will return null.

Documentation made with
Something is wrong in here? Make a issue/pull request on the repo.