Authenticator

This is a dbo authenticator based on the Zend Framework.

Installation

It's recommended that you use Composer to install

$ composer require neilmillard/authenticator

Usage

For use with Slim 3 (for instance)

// database mysqli connection
$container['database'] = function ($c) {
    $settings = $c['settings']['database'];
    $connection = new \PDO($c['dsn'],$settings['username'],$settings['password']);
    return $connection;
};

// authentication
$container['authenticator'] = function ($c) {
    $settings = $c['settings']['authenticator'];
    $connection = $c['database'];
    $adapter = new \Authenticator\Adapter\Db\EloAdapter(
        $connection,
        $settings['tablename'],
        $settings['usernamefield'],
        $settings['credentialfield']
    );
    $authenticator = new \Authenticator\Authenticator($adapter);
    return $authenticator;
};

middleware is provided.

$container['Authenticator\Middleware'] = function ($c) {
    return new \Authenticator\Middleware($c['authenticator'],$c['router'],$c['view']);
};

//authenticator to populate twig view
$app->add('Authenticator\Middleware:addViewData');

in the routes add in the middleware

$app->get('/profile', 'App\Action\ProfileAction:dispatch')
    ->setName('profile')
    ->add('Authenticator\Middleware:auth');

login

$result = $this->authenticator->authenticate($username, $password);
if ($result->isValid()){
    // Success
} else {
    // Authentication error
    $messages = $result->getMessages();
    $error=(string) $messages[0];
}

and to logout

$this->authenticator->clearIdentity();