Coder Social home page Coder Social logo

fgutr / codeigniter-jwt-sample Goto Github PK

View Code? Open in Web Editor NEW

This project forked from paritoshvaidya/codeigniter-jwt-sample

0.0 0.0 0.0 57.04 MB

CodeIgniter JWT Sample

License: MIT License

PHP 96.84% HTML 0.27% Shell 0.30% Makefile 0.02% Python 0.15% CSS 1.26% JavaScript 0.68% Ruby 0.25% BlitzBasic 0.08% Smarty 0.12% Hack 0.01% Batchfile 0.02%

codeigniter-jwt-sample's Introduction

CodeIgniter-JWT-Sample using CodeIgniter 4

Simple CodeIgniter 4 JWT implementation.

Developers who want to use CodeIgniter 3:

Use following command to switch to CodeIgniter 3 branch

git checkout CI3

Setup using this repo

  • Clone this project on php server XAMPP/WAMP.

  • This project uses [Composer] (https://getcomposer.org/) as Dependency Manager

  • Use following command to auto install required dependencies

    composer install

  • In /app/Config/Services.php change secret key at line 23

  • Run or Test code using Postman or any other Rest Client

Setup for existing CodeIgniter 4 project

  • Use composer to manage your dependencies and download PHP-JWT:
 composer require firebase/php-jwt

Copy app/Filters/AuthFilter.php to your project

In /app/Config/Filters.php add following line at the end of $aliases array

'authFilter' => \App\Filters\AuthFilter::class,

Add following in $filters array

'authFilter' => [
    'before' => [
        'api/user/*',
        'api/user',
    ],
],
  • In /app/Config/Routes.php add routes to your Auth controller and resource
$routes->resource('api/auth', ['controller' => 'Auth']);
$routes->resource('api/user', ['controller' => 'User']);
  • In /app/Config/Services.php add function to return secret key
public static function getSecretKey()
{
    return 'example_key';
}
  • Add logic to generate token in your AuthController after 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

Run

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 <Token value from above call>

Project uses

[CodeIgniter 4] (https://www.codeigniter.com/)
[php-jwt] (https://github.com/firebase/php-jwt)

Contact

For any questions mail me [email protected]

GitHub license

codeigniter-jwt-sample's People

Contributors

narfbg avatar lonnieezell avatar jim-parry avatar mgatner avatar samsonasik avatar natanfelles avatar gaker avatar timw4mail avatar atishhamte avatar nowackipawel avatar ericlbarnes avatar vlakoff avatar toopay avatar littlej avatar michalsn avatar alexbilbie avatar ytetsuro avatar kenjis avatar puschie286 avatar dchill42 avatar nyufeng avatar bangbangda avatar akenroberts avatar ridho1991 avatar it-can avatar dafriend avatar aanbar avatar ivantcholakov avatar vkeranov avatar purwandi avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.