PHPackages                             agungsugiarto/codeigniter4-authentication-jwt - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. agungsugiarto/codeigniter4-authentication-jwt

ActiveLibrary[Authentication &amp; Authorization](/categories/authentication)

agungsugiarto/codeigniter4-authentication-jwt
=============================================

JSON Web Token for codeigniter4 authentication.

v1.0.0-beta.2(3y ago)285667[1 issues](https://github.com/agungsugiarto/codeigniter4-authentication-jwt/issues)MITPHPPHP ^7.3|^8.0

Since Mar 28Pushed 3y ago3 watchersCompare

[ Source](https://github.com/agungsugiarto/codeigniter4-authentication-jwt)[ Packagist](https://packagist.org/packages/agungsugiarto/codeigniter4-authentication-jwt)[ Fund](https://saweria.co/agungsugiarto)[ RSS](/packages/agungsugiarto-codeigniter4-authentication-jwt/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (2)Dependencies (8)Versions (3)Used By (0)

CodeIgniter4 Authentication JWT
===============================

[](#codeigniter4-authentication-jwt)

[![Latest Stable Version](https://camo.githubusercontent.com/de1dd285fce15868afc9117c1478958a07b9c452d6cb52dbf411deec63123f20/68747470733a2f2f706f7365722e707567782e6f72672f6167756e67737567696172746f2f636f646569676e69746572342d61757468656e7469636174696f6e2d6a77742f76)](https://github.com/agungsugiarto/codeigniter4-authentication-jwt/releases)[![Total Downloads](https://camo.githubusercontent.com/2c450f64af23a79b6fa7a79185461fe0d3f27165326e75e9ee0eeedd0615e06e/68747470733a2f2f706f7365722e707567782e6f72672f6167756e67737567696172746f2f636f646569676e69746572342d61757468656e7469636174696f6e2d6a77742f646f776e6c6f616473)](https://packagist.org/packages/agungsugiarto/codeigniter4-authentication-jwt/stats)[![Latest Unstable Version](https://camo.githubusercontent.com/56d3ddceb0b2988e893721d410d97d1917cfa0a4eadd848dcf6d809b19f69b45/68747470733a2f2f706f7365722e707567782e6f72672f6167756e67737567696172746f2f636f646569676e69746572342d61757468656e7469636174696f6e2d6a77742f762f756e737461626c65)](https://packagist.org/packages/agungsugiarto/codeigniter4-authentication-jwt)[![License](https://camo.githubusercontent.com/4d746330c0b7f58a1f8028c73c35ef4f7ee5679377ddd1ab819c6e70cc04419e/68747470733a2f2f706f7365722e707567782e6f72672f6167756e67737567696172746f2f636f646569676e69746572342d61757468656e7469636174696f6e2d6a77742f6c6963656e7365)](https://github.com/agungsugiarto/codeigniter4-authentication-jwt/blob/master/LICENSE.md)

About
-----

[](#about)

JSON Web Token for codeigniter4-authentication. This package is port from [tymondesigns/jwt-auth](https://github.com/tymondesigns/jwt-auth) for compability with [agungsugiarto/codeigniter4-authentication](https://github.com/agungsugiarto/codeigniter4-authentication).

Documentation
-------------

[](#documentation)

### Install Via Composer

[](#install-via-composer)

```
composer require agungsugiarto/codeigniter4-authentication-jwt
```

### Copy the config

[](#copy-the-config)

Copy the config file from `vendor/agungsugiarto/codeigniter4-authentication-jwt/src/Config/JWT.php` to config folder of your codeigniter4 application and change class extends from `BaseConfig` to `\Fluent\JWTAuth\Config\JWT`

### Update your User entities

[](#update-your-user-entities)

Firstly you need to implement the `Fluent\JWTAuth\Contracts\JWTSubjectInterface` contract on your User entities, which requires that you implement the 2 methods `getJWTIdentifier()` and `getJWTCustomClaims()`.

The example below should give you an idea of how this could look. Obviously you should make any changes, as necessary, to suit your own needs.

```
namespace App\Entities;

//..
use Fluent\JWTAuth\Contracts\JWTSubjectInterface;

class User extends Entity implements
    //..
    JWTSubjectInterface
{
    /**
     * {@inheritdoc}
     */
    public function getJWTIdentifier()
    {
        return $this->id;
    }

    /**
     * {@inheritdoc}
     */
    public function getJWTCustomClaims()
    {
        return [];
    }
}
```

### Adding `\Fluent\JWTAuth\JWTGuard::class` Guards

[](#adding-fluentjwtauthjwtguardclass-guards)

We need to define `\Fluent\JWTAuth\JWTGuard::class` authentication guards using the `extend` method on the `Auth` facade or service. You should place your call to the `extend` method within a service provider. Since codeigniter4-authentication already ships with an AuthServiceProvider, we can place the code in that provider. Open `\App\Providers\AuthServiceProvider`:

```
namespace App\Providers;

use Fluent\Auth\AbstractServiceProvider;
use Fluent\Auth\Facades\Auth;
use Fluent\JWTAuth\Config\Services;
use Fluent\JWTAuth\JWTGuard;

class AuthServiceProvider extends AbstractServiceProvider
{
    /**
     * {@inheritdoc}
     */
    public static function register()
    {
        Auth::extend(JWTGuard::class, function ($auth, $name, array $config) {
            return new JWTGuard(
                Services::getSharedInstance('jwt'),
                Services::getSharedInstance('request'),
                $auth->createUserProvider($config['provider']),
            );
        });
    }
}
```

### Configure Auth guard

[](#configure-auth-guard)

Inside the `app/Config/Auth.php` file you will need to make a few changes to configure codeigniter4-authentication to use the jwt guard to power your application authentication.

Make the following changes to the file:

```
public $guards = [
    //..
    'api' => [
        'driver' => \Fluent\JWTAuth\JWTGuard::class,
        'provider' => 'users',
    ],
];
```

Here we are telling the api guard to use the `\Fluent\JWTAuth\JWTGuard::class` driver, and we are setting the api guard.

Next we need to register this `App\Providers\AuthServiceProvider` to lifecycle application. Open `App\Config\Events` add this line:

```
Events::on('pre_system', [\App\Providers\AuthServiceProvider::class, 'register']);
```

We can now use codeigniter4-authentication built in Auth system, with codeigniter4-authentication-jwt doing the work behind the scenes!

### Add some basic authentication routes

[](#add-some-basic-authentication-routes)

First let's add some routes in app/Config/Routes.php as follows:

```
$routes->group('jwt', function ($routes) {
    $routes->post('login', 'JwtauthController::login');
    $routes->post('logout', 'JwtauthController::logout', ['filter' => 'auth:api']);
    $routes->post('refresh', 'JwtauthController::refresh', ['filter' => 'auth:api']);
    $routes->match(['get', 'post'], 'user', 'JwtauthController::user', ['filter' => 'auth:api']);
});
```

### Create the AuthController

[](#create-the-authcontroller)

Then create the `JwtauthController`, either manually or by running the spark command:

```
php spark make:controller JwtauthController
```

Then add the following:

```
