CodeIgniter JWT Sample
Simple CodeIgniter 4 JWT implementation.
Use following command to switch to CodeIgniter 3 branch
git checkout CI3
Clone this project on php server XAMPP/WAMP.
This project uses Composer as Dependency Manager
Use following command to auto install required dependencies
composer install
In
/app/Config/Services.phpchange
secret keyat
line 23
Run or Test code using Postman or any other Rest Client
composer require firebase/php-jwt
Copy
app/Filters/AuthFilter.phpto your project
In
/app/Config/Filters.phpadd following line at the end of
$aliasesarray
'authFilter' => \App\Filters\AuthFilter::class,
Add following in
$filtersarray
'authFilter' => [ 'before' => [ 'api/user/*', 'api/user', ], ],
/app/Config/Routes.phpadd routes to your Auth controller and resource
$routes->resource('api/auth', ['controller' => 'Auth']); $routes->resource('api/user', ['controller' => 'User']);
/app/Config/Services.phpadd function to return secret key
public static function getSecretKey() { return 'example_key'; }
AuthControllerafter validating login credentials ``` $key = Services::getSecretKey(); $payload = array( "iss" => "http://example.org", "aud" => "http://example.com", "iat" => 1356999524, "nbf" => 1357000000 );
$jwt = JWT::encode($payload, $key); ``` - Run or Test code using Postman or any other Rest Client
Generate auth token using login credentials
URL: http://localhost/CodeIgniter-JWT-Sample/public/index.php/api/auth Method: POST Params type: x-www-form-urlencoded Params: email:same_text password:same_text
Access resource - User for this example
URL: http://localhost/CodeIgniter-JWT-Sample/public/index.php/api/user Method: GET Header Key: Authorization Value: Bearer
For any questions mail me [email protected]