PHP code example of dez-php / phalcon-auth

1. Go to this page and download the library: Download dez-php/phalcon-auth library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

dez-php / phalcon-auth example snippets




$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;
});

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

try{
    $auth->authenticate('[email protected]', 'qwerty');
}catch (\Exception $e){
    echo "You have some errors: {$e->getMessage()}";
}

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

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

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

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

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