dez-php/phalcon-auth

Auth Component for Phalcon 2 Framework

v0.2.3 2016-06-08 21:09 UTC

This package is not auto-updated.

Last update: 2024-12-15 08:31:10 UTC


README

Auth Component for Phalcon 2.x Framework

#Usage

Register Auth in dependency injection container

include_once '../vendor/autoload.php';

$container  = new \Phalcon\Di\FactoryDefault();

$container->set('auth', function(){
    
    // Pass session adapter into Auth
    $auth   = new \PhalconDez\Auth\Auth(
        new \PhalconDez\Auth\Adapter\Session()
    );
    
    // Pass empty instance of Credentials Model implement PhalconModel
    $auth->setCredentialsModel(
        new \PhalconDez\Auth\Model\Credentials()
    );
    
    // Pass empty instance of Session Model implement PhalconModel
    $auth->setSessionModel(
        new \PhalconDez\Auth\Model\Session()
    );
    
    // Run initialize
    $auth->initialize();
    
    return $auth;
});

and then fetch auth from container

/** @var \PhalconDez\Auth\Auth $auth */
$auth   = $container->get('auth');

authorization

try{
    $auth->authenticate('test@gmail.com', 'qwerty');
}catch (\Exception $e){
    echo "You have some errors: {$e->getMessage()}";
}

creating new credentials

try{
    $auth->authenticate($email, $password);
}catch (\Exception $e){
    $auth->create($email, $password);
    $container->get('response')->redirect('auth-page');
}

verify password

if($auth->isUser() && $auth->getAdapter()->verifyPassword('qwerty') === true){
    echo 'Password is corrected';
}

check user user authorization

if($auth->isGuest() === true){
    echo 'You are non-authorized user';
}

if($auth->isUser() === true){
    echo 'You are authorized user';
}

get some authorized user data

if($auth->isUser() === true){
    $userModel  = $auth->getUser();
    echo "Hello, {$userModel->getEmail()}. You was registered at {$userModel->getCreatedAt()}";
}

#Issues and pull-request

####I will be glad of your criticism, tasks, bugs and pull-request.