PHPackages                             sametsahindogan/laravel-jwtredis - 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. sametsahindogan/laravel-jwtredis

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

sametsahindogan/laravel-jwtredis
================================

This package allows JWT-authenticated users to be stored and management in Redis with their roles, permissions, statuses and anything you want.

v1.2.0(5y ago)1221.9k↓50%7MITPHPPHP &gt;=7.2.5

Since Jan 16Pushed 5y ago7 watchersCompare

[ Source](https://github.com/sametsahindogan/laravel-jwtredis)[ Packagist](https://packagist.org/packages/sametsahindogan/laravel-jwtredis)[ RSS](/packages/sametsahindogan-laravel-jwtredis/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (3)Versions (21)Used By (0)

laravel-jwtredis
================

[](#laravel-jwtredis)

[![](https://camo.githubusercontent.com/916fcb613cec7b43f119f48c2dc2f97914090065c31e425e05cb7aea27132d35/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f73616d6574736168696e646f67616e2f6c61726176656c2d6a77747265646973)](https://packagist.org/packages/sametsahindogan/laravel-jwtredis)[![](https://camo.githubusercontent.com/c0f94c324cb26bdeae4d6f76d98bbba9a53c513c4f870718e07e2821339b5ef6/68747470733a2f2f6769746875622e7374796c6563692e696f2f7265706f732f3233343330343530352f736869656c643f6272616e63683d6d6173746572)](https://github.styleci.io/repos/234304505)[![](https://camo.githubusercontent.com/7004b5443b5c78692ce034c13dcce95fc394fa0449a23c82bd95de8f1d01c1cb/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f73616d6574736168696e646f67616e2f6c61726176656c2d6a77747265646973)](https://raw.githubusercontent.com/sametsahindogan/laravel-jwtredis/master/LICENSE)[![](https://camo.githubusercontent.com/6bc083d3aeb2237b60a3142705d3bef76ddce7c7a63e913571fded13b2e44def/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f73616d6574736168696e646f67616e2f6c61726176656c2d6a777472656469733f636f6c6f723d73756363657373)](https://packagist.org/packages/sametsahindogan/laravel-jwtredis)

> This package allows JWT-authenticated users to be stored and management in Redis with their roles, permissions, statuses and anything you want.

[![](https://camo.githubusercontent.com/81db71691478bb110f72fc93efe262652fded4eff25455ba761c1c1fd169d45a/68747470733a2f2f63646e2e61757468302e636f6d2f626c6f672f6a7774616c676f732f6c6f676f2e706e67)](https://camo.githubusercontent.com/81db71691478bb110f72fc93efe262652fded4eff25455ba761c1c1fd169d45a/68747470733a2f2f63646e2e61757468302e636f6d2f626c6f672f6a7774616c676f732f6c6f676f2e706e67)[![](https://camo.githubusercontent.com/81971fca284cadaa65c11db2bc16855c48b3a680ee89cdf2dace2c6352293113/68747470733a2f2f63646e342e69636f6e66696e6465722e636f6d2f646174612f69636f6e732f72656469732d322f313435312f556e7469746c65642d322d3531322e706e67)](https://camo.githubusercontent.com/81971fca284cadaa65c11db2bc16855c48b3a680ee89cdf2dace2c6352293113/68747470733a2f2f63646e342e69636f6e66696e6465722e636f6d2f646174612f69636f6e732f72656469732d322f313435312f556e7469746c65642d322d3531322e706e67)

> Also this package have an observer for listening and updating to your user model on Redis. This observer is triggered `when you assign roles & permissions to user, or update and delete to your user` model.

Requirements
------------

[](#requirements)

This package work with together [tymondesigns/jwt-auth](https://github.com/tymondesigns/jwt-auth) and [spatie/laravel-permission](https://github.com/spatie/laravel-permission) package under the hood.

!`Make sure to install and configure these dependencies. You must publish, migrate etc. all packages.`!

- If your PHP setup does not have the [Redis](https://github.com/phpredis/phpredis) extension or Laravel's Redis driver is not selected as [phpredis](https://laravel.com/docs/7.x/redis#phpredis), you must install [nrk/predis](https://github.com/nrk/predis) **&gt;= 1.1** (**Recommended 1.1**)
- PHP **&gt;= 7.2.5**
- Laravel **&gt;= 6.0**
- [tymondesigns/jwt-auth](https://github.com/tymondesigns/jwt-auth) **&gt;= 1.0** (**Recommended 1.0.2**)
- [spatie/laravel-permission](https://github.com/spatie/laravel-permission) **&gt;= 4.0** (**Recommended 4.0.0**)
- [sametsahindogan/response-object-creator](https://github.com/sametsahindogan/response-object-creator) **&gt;= 1.0** (**Recommended 1.0.2**)

Installation
------------

[](#installation)

```
composer require sametsahindogan/laravel-jwtredis
```

Once this has finished, you will need to add&amp;change these values in `.env` file:

```
CACHE_DRIVER=redis
REDIS_CLIENT=predis
```

Next, you will need to change the `guards` and `providers` arrays in your `config/auth.php` config as follows:

```
'guards' => [
        'api' => [
            'driver' => 'jwt_redis_guard',
            'provider' => 'users'
        ],
    ],

'providers' => [
        'users' => [
            'driver' => 'jwt_redis_user_provider',
            'model' =>  App\User::class, // Your User Model
        ],
    ],
```

This package uses auto-discovery to register the service provider but if you'd rather do it manually, the service provider is: add to `providers` array in your `config/app.php` config as follows:

```
Sametsahindogan\JWTRedis\JWTRedisServiceProvider::class,
```

You will want to publish the config using the following command:

```
php artisan vendor:publish --provider='Sametsahindogan\JWTRedis\JWTRedisServiceProvider'
```

Configurations
--------------

[](#configurations)

When everything is done, don't forget to add this Trait to your user model.

```
use JWTRedisHasRoles;
```

You need to add `$routeMiddleware` array in `app/Http/Kernel.php`

```
'auth'               => \Sametsahindogan\JWTRedis\Http\Middleware\Authenticate::class,
'refreshable'        => \Sametsahindogan\JWTRedis\Http\Middleware\Refreshable::class,
'role'               => \Sametsahindogan\JWTRedis\Http\Middleware\RoleMiddleware::class,
'permission'         => \Sametsahindogan\JWTRedis\Http\Middleware\PermissionMiddleware::class,
'role_or_permission' => \Sametsahindogan\JWTRedis\Http\Middleware\RoleOrPermissionMiddleware::class,
```

Usage
-----

[](#usage)

You do not have any instructions for use. This package only affects the background, functions in an almost identical way to Laravel session authentication, with a few exceptions. `All you have to do is change your middleware.(I mention this below)` You can use Laravel's Auth facade, Tymon's JWTAuth facade and all [spatie/laravel-permission](https://github.com/spatie/laravel-permission) package methods as usual.

- For user authentication by token;
    ( Use this middleware if the user's identity is not important. This middleware only checks if Token is valid. Doesn't send to any query to any database.)

```
Route::get("/example", "ExampleController@example")->middleware('auth');
```

- To check user authorization, you need to this one of these middlewares;
    ( Use this middleware if the user's identity is important. This middlewares fetch user from Redis and mark as authorized to Laravel's Request object. And you will reach all default Auth facade's methods you want. Just call Laravel's Auth facades.)

```
Route::get("/example", "ExampleController@example")->middleware('role:admin|user');
Route::get("/example", "ExampleController@example")->middleware('permissions:get-user|set-user');
Route::get("/example", "ExampleController@example")->middleware('role_or_permission:admin|get-user');
```

- To refresh the token, you can add the `refreshable` middleware to the required route. You don't need to take any action on the controller of this route;
    ( Also this middleware can refreshes user from Redis if necessary. )

```
Route::get("/example", "ExampleController@example")->middleware('refreshable');
```

`If you want to do different things, you can override those mentioned middlewares.`

**After using it as above, every authorization you made in your application, such as `Auth::user()` or `$user->can('permission')`, is always checked from Redis, not from the database.**

Options
-------

[](#options)

You can customize some options in that package. Check `config/jwtredis.php` file.

- User Model

```
    /*
    |--------------------------------------------------------------------------
    | Your User Model
    |--------------------------------------------------------------------------
    |
    | You can set specific user model.
    |
    */
    'user_model' => \App\Models\User::class,
```

- Observer

```
     /*
     |--------------------------------------------------------------------------
     | JWTRedis User Model Observer
     |--------------------------------------------------------------------------
     |
     | This observer class, listening all events on your user model. Is triggered
     | when you assign roles & permissions to user, or update and delete to
     | your user model.
     |
     */
    'observer' => \Sametsahindogan\JWTRedis\Observers\UserRedisObserver::class,
```

- Events Queue

```
    /*
    |--------------------------------------------------------------------------
    | Observer Events Are Queued
    |--------------------------------------------------------------------------
    |
    | If this option is true, model's events are processed as a job on queue.
    | The job will be executed after the database transactions are commit.
    |
    | * ~ Don't forget to run Queue Worker if this option is true. ~ *
    |
    */
    'observer_events_queue' => env('JWTREDIS_OBSERVER_EVENTS_QUEUE', false),
```

- Cache Time

```
    /*
    |--------------------------------------------------------------------------
    | Cache on Redis up to jwt_ttl value.
    |--------------------------------------------------------------------------
    |
    | If it's option is true, user stored in Redis up to jwt_ttl value time.
    |
    */
    'redis_ttl_jwt' => true,

    /*
    |--------------------------------------------------------------------------
    | Cache on Redis up to specific time
    |--------------------------------------------------------------------------
    |
    | If you don't want to store user in Redis until JWT expire time,
    | you can set this value as minute.
    |
    */
    'redis_ttl' => env('JWTREDIS_REDIS_TTL', 60),
```

- Cache Prefix

```
    /*
    |--------------------------------------------------------------------------
    | Cache Prefix
    |--------------------------------------------------------------------------
    |
    | If it's user id is 1, this user stored in Redis as auth_1.
    |
    */
    'redis_auth_prefix' => env('JWTREDIS_REDIS_AUTH_PREFIX', 'auth_'),
```

- Serialization

```
    /*
   |--------------------------------------------------------------------------
   | Igbinary Serialization
   |--------------------------------------------------------------------------
   |
   | Igbinary Serialization provides a better performance and lower memory
   | usage than PHP Serialization.
   |
   | * ~ Don't forget to enable igbinary extension if this option is true. ~ *
   |
   */
    'igbinary_serialization' => env('JWTREDIS_IGBINARY_SERIALIZATION', false),
```

- Banned User Check

```
    /*
    |--------------------------------------------------------------------------
    | Banned User Checking
    |--------------------------------------------------------------------------
    |
    | If the check_banned_user option is true, that users cannot access
    | the your application.
    |
    */
    'check_banned_user' => env('JWTREDIS_CHECK_BANNED_USER', false),

    /*
    |--------------------------------------------------------------------------
    | Status Column For Banned User Checking
    |--------------------------------------------------------------------------
    |
    | You can set your specific column name of your user model.
    |
    */
    'status_column_title' => 'status',

    /*
    |--------------------------------------------------------------------------
    | Restricted statuses For Banned User Checking
    |--------------------------------------------------------------------------
    |
    | If the user has one of these statuses and trying to reach your application,
    | JWTRedis throws AccountBlockedException.
    | You can set the message (check it errors array) that will return in this
    | exception.
    |
    */
    'banned_statuses' => [
        'banned',
        'deactivate'
    ],
```

- Relation Caching

```
    /*
    |--------------------------------------------------------------------------
    | Cache This Relations When User Has Authenticated
    |--------------------------------------------------------------------------
    |
    | You can add this array to your own relations, anything you want to store
    | in Redis. We recommend caching only roles and permissions here as much as
    | possible.
    |
    */
    'cache_relations' => [
        'roles.permissions',
        'permissions'
    ],
```

- Customize Exceptions

```
    /*
    |--------------------------------------------------------------------------
    | Customize All Exception Messages and Codes
    |--------------------------------------------------------------------------
    |
    | You can customize error code,message,title for your application.
    |
    */
    'errors' => [
       'TokenNotProvidedException' => [
           'title' => 'Your custom title',
           'message' => 'Your custom error message.',
           'code' => 99999
       ]
    ]
```

Example Project
---------------

[](#example-project)

Here is an [example](https://github.com/sametsahindogan/laravel-jwtredis-example) using laravel-jwtredis. You can examine in detail.

Performance Improvements Tips
-----------------------------

[](#performance-improvements-tips)

You may install the PhpRedis PHP extension via PECL. The extension is more complex to install but may yield better performance for applications that make heavy use of Redis. Predis is the alternative for PhpRedis on pure PHP and does not require any additional C extension by default.

"PhpRedis is faster about x6 times. Using igbinary serializer reduces stored data size about 3x times. If Redis installed on separate machines, reducing network traffic is a very significant speedup."

In my opinion, using [PhpRedis](https://github.com/phpredis/phpredis) and serializer as igbinary ( Laravel does not support igbinary serialization on Redis. However, this package provides igbinary serialization support for Laravel. Please check `config/jwtredis.php` file. ) in production environment gives a great performance.

You can review this [article](https://medium.com/@akalongman/phpredis-vs-predis-comparison-on-real-production-data-a819b48cbadb) for performance comparison [PhpRedis](https://github.com/phpredis/phpredis) vs. [Predis](https://github.com/nrk/predis).

Integrate with [Laravel Swoole Package](https://github.com/swooletw/laravel-swoole)
-----------------------------------------------------------------------------------

[](#integrate-with-laravel-swoole-package)

This package fully compatible with [Laravel Swoole](https://github.com/swooletw/laravel-swoole) package. If you want to use it together, all you have to do is change the `instances` and `providers` arrays in your `config/swoole_http.php` config as follows:

```
    /*
    |--------------------------------------------------------------------------
    | Instances here will be cleared on every request.
    |--------------------------------------------------------------------------
    */
    'instances' => [
        'auth'
    ],

    /*
    |--------------------------------------------------------------------------
    | Providers here will be registered on every request.
    |--------------------------------------------------------------------------
    */
    'providers' => [
        \Sametsahindogan\JWTRedis\JWTRedisServiceProvider::class,
    ],
```

License
-------

[](#license)

MIT © [Samet Sahindogan](https://github.com/sametsahindogan/laravel-jwtredis/blob/master/LICENSE)

###  Health Score

35

—

LowBetter than 80% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity34

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity61

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~21 days

Recently: every ~49 days

Total

20

Last Release

1902d ago

PHP version history (2 changes)v1.0.0PHP &gt;=7.2

v1.2.0PHP &gt;=7.2.5

### Community

Maintainers

![](https://www.gravatar.com/avatar/473ceed3f0cea57f7ea1e773f16ecaf17abdc40278285db38fc8ebdbd0599419?d=identicon)[sametsahindogan](/maintainers/sametsahindogan)

---

Top Contributors

[![sametsahindogan](https://avatars.githubusercontent.com/u/36247856?v=4)](https://github.com/sametsahindogan "sametsahindogan (75 commits)")

---

Tags

facadesigbinaryjwtjwt-tokenlaravellaravel-frameworklaravel-jwtredislaravel-permissionlaravel-swoolemiddlewarephpphp7php8phpredisredisspatieswooletymondesignsuser-identitylaravelAuthenticationrediscacheauthorizationpermissionrolespermissions

### Embed Badge

![Health badge](/badges/sametsahindogan-laravel-jwtredis/health.svg)

```
[![Health](https://phpackages.com/badges/sametsahindogan-laravel-jwtredis/health.svg)](https://phpackages.com/packages/sametsahindogan-laravel-jwtredis)
```

###  Alternatives

[bezhansalleh/filament-shield

Filament support for `spatie/laravel-permission`.

2.8k2.9M88](/packages/bezhansalleh-filament-shield)[pktharindu/nova-permissions

Laravel Nova Grouped Permissions (ACL)

136387.1k](/packages/pktharindu-nova-permissions)[silvanite/novatoolpermissions

Laravel Nova Permissions (Roles and Permission based Access Control (ACL))

100256.7k2](/packages/silvanite-novatoolpermissions)[hasinhayder/tyro

Tyro - The ultimate Authentication, Authorization, and Role &amp; Privilege Management solution for Laravel 12 &amp; 13

6712.1k2](/packages/hasinhayder-tyro)[hosseinhezami/laravel-permission-manager

Advanced permission manager for Laravel.

403.3k](/packages/hosseinhezami-laravel-permission-manager)[tonystore/livewire-permission-manager

Package that provides a graphical interface to manage roles and permissions.

171.0k](/packages/tonystore-livewire-permission-manager)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
