{"id":13609562,"url":"https://github.com/sametsahindogan/laravel-jwtredis","last_synced_at":"2025-05-07T09:46:07.590Z","repository":{"id":62539786,"uuid":"234304505","full_name":"sametsahindogan/laravel-jwtredis","owner":"sametsahindogan","description":":rocket: This package allows JWT-authenticated users to be stored and management in Redis with their roles, permissions, statuses and anything you want.","archived":false,"fork":false,"pushed_at":"2021-05-10T05:37:37.000Z","size":116,"stargazers_count":124,"open_issues_count":0,"forks_count":7,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-04-21T17:58:09.582Z","etag":null,"topics":["facades","igbinary","jwt","jwt-token","laravel","laravel-framework","laravel-jwtredis","laravel-permission","laravel-swoole","middleware","php","php7","php8","phpredis","redis","spatie","swoole","tymondesigns","user-identity"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sametsahindogan.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-01-16T11:23:02.000Z","updated_at":"2024-08-04T19:36:57.000Z","dependencies_parsed_at":"2022-11-02T16:16:19.622Z","dependency_job_id":null,"html_url":"https://github.com/sametsahindogan/laravel-jwtredis","commit_stats":null,"previous_names":[],"tags_count":20,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sametsahindogan%2Flaravel-jwtredis","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sametsahindogan%2Flaravel-jwtredis/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sametsahindogan%2Flaravel-jwtredis/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sametsahindogan%2Flaravel-jwtredis/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sametsahindogan","download_url":"https://codeload.github.com/sametsahindogan/laravel-jwtredis/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252854329,"owners_count":21814668,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["facades","igbinary","jwt","jwt-token","laravel","laravel-framework","laravel-jwtredis","laravel-permission","laravel-swoole","middleware","php","php7","php8","phpredis","redis","spatie","swoole","tymondesigns","user-identity"],"created_at":"2024-08-01T19:01:35.969Z","updated_at":"2025-05-07T09:46:07.570Z","avatar_url":"https://github.com/sametsahindogan.png","language":"PHP","funding_links":[],"categories":["PHP"],"sub_categories":[],"readme":"# laravel-jwtredis\n\n[![](https://img.shields.io/packagist/v/sametsahindogan/laravel-jwtredis)](https://packagist.org/packages/sametsahindogan/laravel-jwtredis)\n[![](https://github.styleci.io/repos/234304505/shield?branch=master)](https://github.styleci.io/repos/234304505)\n[![](https://img.shields.io/packagist/l/sametsahindogan/laravel-jwtredis)](https://raw.githubusercontent.com/sametsahindogan/laravel-jwtredis/master/LICENSE)\n[![](https://img.shields.io/packagist/dt/sametsahindogan/laravel-jwtredis?color=success)](https://packagist.org/packages/sametsahindogan/laravel-jwtredis)\n\n\u003e This package allows JWT-authenticated users to be stored and management in Redis with\ntheir roles, permissions, statuses and anything you want.\n\n\u003ca\u003e\u003cimg src=\"https://cdn.auth0.com/blog/jwtalgos/logo.png\" width=\"80\"\u003e\u003c/a\u003e\n\u003ca\u003e\u003cimg src=\"https://cdn4.iconfinder.com/data/icons/redis-2/1451/Untitled-2-512.png\" width=\"80\"\u003e\u003c/a\u003e\n\n\u003eAlso this package have an observer for listening and updating  to your user model \non Redis. This observer is triggered `when you assign roles \u0026 permissions to user, or update\nand delete to your user` model.\n\n## Requirements\nThis 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.\n\n!`Make sure to install and configure these dependencies. You must publish, migrate etc. all packages.`!\n \n- 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)  **\u003e= 1.1** (**Recommended 1.1**)\n- PHP **\u003e= 7.2.5**\n- Laravel **\u003e= 6.0**\n- [tymondesigns/jwt-auth](https://github.com/tymondesigns/jwt-auth) **\u003e= 1.0** (**Recommended 1.0.2**)\n- [spatie/laravel-permission](https://github.com/spatie/laravel-permission) **\u003e= 4.0** (**Recommended 4.0.0**)\n- [sametsahindogan/response-object-creator](https://github.com/sametsahindogan/response-object-creator) **\u003e= 1.0** (**Recommended 1.0.2**)\n\n## Installation\n```bash\ncomposer require sametsahindogan/laravel-jwtredis\n```\nOnce this has finished, you will need to add\u0026change these values in `.env` file:\n```dotenv\nCACHE_DRIVER=redis\nREDIS_CLIENT=predis\n```\nNext, you will need to change the `guards` and `providers` arrays in your `config/auth.php` config as follows:\n```php\n'guards' =\u003e [\n        'api' =\u003e [\n            'driver' =\u003e 'jwt_redis_guard',\n            'provider' =\u003e 'users'\n        ],\n    ],\n\n'providers' =\u003e [\n        'users' =\u003e [\n            'driver' =\u003e 'jwt_redis_user_provider',\n            'model' =\u003e  App\\User::class, // Your User Model\n        ],\n    ],\n```\nThis 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:\n```php\nSametsahindogan\\JWTRedis\\JWTRedisServiceProvider::class,\n```\nYou will want to publish the config using the following command:\n```bash\nphp artisan vendor:publish --provider='Sametsahindogan\\JWTRedis\\JWTRedisServiceProvider'\n```\n\n## Configurations\nWhen everything is done, don't forget to add this Trait to your user model.\n```php\nuse JWTRedisHasRoles;\n```\nYou need to add `$routeMiddleware` array in `app/Http/Kernel.php`\n```php\n'auth'               =\u003e \\Sametsahindogan\\JWTRedis\\Http\\Middleware\\Authenticate::class,\n'refreshable'        =\u003e \\Sametsahindogan\\JWTRedis\\Http\\Middleware\\Refreshable::class,\n'role'               =\u003e \\Sametsahindogan\\JWTRedis\\Http\\Middleware\\RoleMiddleware::class,\n'permission'         =\u003e \\Sametsahindogan\\JWTRedis\\Http\\Middleware\\PermissionMiddleware::class,\n'role_or_permission' =\u003e \\Sametsahindogan\\JWTRedis\\Http\\Middleware\\RoleOrPermissionMiddleware::class,\n```\n\n## Usage\n 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,\nTymon's JWTAuth facade and all [spatie/laravel-permission](https://github.com/spatie/laravel-permission) package methods as usual.\u003cbr\u003e\n\n* For user authentication by token; \u003cbr\u003e\n( 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.)\n```php\nRoute::get(\"/example\", \"ExampleController@example\")-\u003emiddleware('auth');\n```\n* To check user authorization, you need to this one of these middlewares;\u003cbr\u003e\n( Use this middleware if the user's identity is important. This middlewares fetch user from Redis and mark as authorized \nto Laravel's Request object. And you will reach all default Auth facade's methods you want. Just call Laravel's Auth facades.)\n```php\nRoute::get(\"/example\", \"ExampleController@example\")-\u003emiddleware('role:admin|user');\nRoute::get(\"/example\", \"ExampleController@example\")-\u003emiddleware('permissions:get-user|set-user');\nRoute::get(\"/example\", \"ExampleController@example\")-\u003emiddleware('role_or_permission:admin|get-user');\n```\n* 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;\u003cbr\u003e\n( Also this middleware can refreshes user from Redis if necessary. )\n```php\nRoute::get(\"/example\", \"ExampleController@example\")-\u003emiddleware('refreshable');\n```\n\u003cbr\u003e\n\n`If you want to do different things, you can override those mentioned middlewares.`\n\n**After using it as above, every authorization you made in your \napplication, such as `Auth::user()` or `$user-\u003ecan('permission')`, is always checked from Redis, not from the database.**\n\n## Options\nYou can customize some options in that package. Check `config/jwtredis.php` file.\n\n* User Model\n```php\n    /*\n    |--------------------------------------------------------------------------\n    | Your User Model\n    |--------------------------------------------------------------------------\n    |\n    | You can set specific user model.\n    |\n    */\n    'user_model' =\u003e \\App\\Models\\User::class,\n```\n* Observer\n```php\n     /*\n     |--------------------------------------------------------------------------\n     | JWTRedis User Model Observer\n     |--------------------------------------------------------------------------\n     |\n     | This observer class, listening all events on your user model. Is triggered\n     | when you assign roles \u0026 permissions to user, or update and delete to\n     | your user model.\n     |\n     */\n    'observer' =\u003e \\Sametsahindogan\\JWTRedis\\Observers\\UserRedisObserver::class,\n```\n* Events Queue\n```php\n    /*\n    |--------------------------------------------------------------------------\n    | Observer Events Are Queued\n    |--------------------------------------------------------------------------\n    |\n    | If this option is true, model's events are processed as a job on queue.\n    | The job will be executed after the database transactions are commit.\n    |\n    | * ~ Don't forget to run Queue Worker if this option is true. ~ *\n    |\n    */\n    'observer_events_queue' =\u003e env('JWTREDIS_OBSERVER_EVENTS_QUEUE', false),\n```\n* Cache Time\n```php\n    /*\n    |--------------------------------------------------------------------------\n    | Cache on Redis up to jwt_ttl value.\n    |--------------------------------------------------------------------------\n    |\n    | If it's option is true, user stored in Redis up to jwt_ttl value time.\n    |\n    */\n    'redis_ttl_jwt' =\u003e true,\n\n    /*\n    |--------------------------------------------------------------------------\n    | Cache on Redis up to specific time\n    |--------------------------------------------------------------------------\n    |\n    | If you don't want to store user in Redis until JWT expire time, \n    | you can set this value as minute.\n    |\n    */\n    'redis_ttl' =\u003e env('JWTREDIS_REDIS_TTL', 60),\n```\n* Cache Prefix\n```php\n    /*\n    |--------------------------------------------------------------------------\n    | Cache Prefix\n    |--------------------------------------------------------------------------\n    |\n    | If it's user id is 1, this user stored in Redis as auth_1.\n    |\n    */\n    'redis_auth_prefix' =\u003e env('JWTREDIS_REDIS_AUTH_PREFIX', 'auth_'),\n```\n* Serialization\n```php\n    /*\n   |--------------------------------------------------------------------------\n   | Igbinary Serialization\n   |--------------------------------------------------------------------------\n   |\n   | Igbinary Serialization provides a better performance and lower memory \n   | usage than PHP Serialization.\n   |\n   | * ~ Don't forget to enable igbinary extension if this option is true. ~ *\n   |\n   */\n    'igbinary_serialization' =\u003e env('JWTREDIS_IGBINARY_SERIALIZATION', false),\n```\n* Banned User Check\n```php\n    /*\n    |--------------------------------------------------------------------------\n    | Banned User Checking\n    |--------------------------------------------------------------------------\n    |\n    | If the check_banned_user option is true, that users cannot access\n    | the your application.\n    |\n    */\n    'check_banned_user' =\u003e env('JWTREDIS_CHECK_BANNED_USER', false),\n\n    /*\n    |--------------------------------------------------------------------------\n    | Status Column For Banned User Checking\n    |--------------------------------------------------------------------------\n    |\n    | You can set your specific column name of your user model.\n    |\n    */\n    'status_column_title' =\u003e 'status',\n\n\n    /*\n    |--------------------------------------------------------------------------\n    | Restricted statuses For Banned User Checking\n    |--------------------------------------------------------------------------\n    |\n    | If the user has one of these statuses and trying to reach your application,\n    | JWTRedis throws AccountBlockedException.\n    | You can set the message (check it errors array) that will return in this\n    | exception.\n    |\n    */\n    'banned_statuses' =\u003e [\n        'banned',\n        'deactivate'\n    ],\n```\n* Relation Caching\n```php\n    /*\n    |--------------------------------------------------------------------------\n    | Cache This Relations When User Has Authenticated\n    |--------------------------------------------------------------------------\n    |\n    | You can add this array to your own relations, anything you want to store\n    | in Redis. We recommend caching only roles and permissions here as much as\n    | possible.\n    |\n    */\n    'cache_relations' =\u003e [\n        'roles.permissions',\n        'permissions'\n    ],\n```\n* Customize Exceptions\n```php\n    /*\n    |--------------------------------------------------------------------------\n    | Customize All Exception Messages and Codes\n    |--------------------------------------------------------------------------\n    |\n    | You can customize error code,message,title for your application.\n    |\n    */\n    'errors' =\u003e [\n       'TokenNotProvidedException' =\u003e [\n           'title' =\u003e 'Your custom title',\n           'message' =\u003e 'Your custom error message.',\n           'code' =\u003e 99999\n       ]\n    ]\n```\n\n## Example Project\nHere is an [example](https://github.com/sametsahindogan/laravel-jwtredis-example) using laravel-jwtredis. You can examine in detail.\n\n## Performance Improvements Tips\n\nYou 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.\n\n\"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.\"\n\nIn 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.\n\nYou 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).\n\n## Integrate with [Laravel Swoole Package](https://github.com/swooletw/laravel-swoole)\nThis 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:\n\n```php\n    /*\n    |--------------------------------------------------------------------------\n    | Instances here will be cleared on every request.\n    |--------------------------------------------------------------------------\n    */\n    'instances' =\u003e [\n        'auth'\n    ],\n\n    /*\n    |--------------------------------------------------------------------------\n    | Providers here will be registered on every request.\n    |--------------------------------------------------------------------------\n    */\n    'providers' =\u003e [\n        \\Sametsahindogan\\JWTRedis\\JWTRedisServiceProvider::class,\n    ],\n```\n\n## License\nMIT © [Samet Sahindogan](https://github.com/sametsahindogan/laravel-jwtredis/blob/master/LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsametsahindogan%2Flaravel-jwtredis","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsametsahindogan%2Flaravel-jwtredis","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsametsahindogan%2Flaravel-jwtredis/lists"}