1. Go to this page and download the library: Download dmkit/phalcon-jwt-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/ */
dmkit / phalcon-jwt-auth example snippets
use Phalcon\Mvc\Micro;
use Phalcon\Config\Adapter\Ini as ConfigIni;
use Phalcon\Di\FactoryDefault;
use Dmkit\Phalcon\Auth\Middleware\Micro as AuthMicro;
// set default services
$di = new FactoryDefault();
/**
* IMPORTANT:
* You must set "config" service that will load the configuration file.
*/
$config = new ConfigIni( APP_PATH . "app/config/config.ini");
$di->set(
"config",
function () use($config) {
return $config;
}
);
$app = new Micro($di);
// AUTH MICRO
$auth = new AuthMicro($app);
$app->handle();
use Phalcon\Mvc\Micro;
use Phalcon\Config\Adapter\Ini as ConfigIni;
use Phalcon\Di\FactoryDefault;
use Dmkit\Phalcon\Auth\Middleware\Micro as AuthMicro;
// set default services
$di = new FactoryDefault();
$app = new Micro($di);
// SETUP THE CONFIG
$authConfig = [
'secretKey' => '923753F2317FC1EE5B52DF23951B1',
'payload' => [
'exp' => 1440,
'iss' => 'phalcon-jwt-auth'
],
'ignoreUri' => [
'/',
'regex:/application/',
'regex:/users/:POST,PUT',
'/auth/user:POST,PUT',
'/auth/application'
]
];
// AUTH MICRO
$auth = new AuthMicro($app, $authConfig);
$app->handle();
$auth->onUnauthorized(function($authMicro, $app) {
$response = $app["response"];
$response->setStatusCode(401, 'Unauthorized');
$response->setContentType("application/json");
// to get the error messages
$response->setContent(json_encode([$authMicro->getMessages()[0]]));
$response->send();
// return false to stop the execution
return false;
});
$auth->onCheck(function($auth) {
// to get the payload
$data = $auth->data();
if($data['iat'] <= strtotime('-1 day')) ) {
// return false to invalidate the authentication
return false;
}
});
print_r( $app['auth']->data() );
print_r( $app->getDI()->get('auth')->data('email') );
// in your contoller
print_r( $this->auth->data() );
echo $this->auth->id(); // will look for sub or id payload
echo $this->auth->data(); // return all payload
echo $this->auth->data('email');
Dmkit\Phalcon\Auth\Auth.php and its adapters - does all the authentication
Dmkit\Phalcon\Auth\TokenGetter\TokenGetter.php and its adapters - does the parsing or getting of token
$ phpunit --configuration phpunit.xml.dist
PHPUnit 5.6.5 by Sebastian Bergmann and contributors.
......["missing token"].["members option"].["members put"].["members put"].["Expired token"].["members post"].... 15 / 15 (100%)
Time: 73 ms, Memory: 10.00MB
OK (15 tests, 27 assertions)
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.