{"id":14985450,"url":"https://github.com/irazasyed/jwt-auth-guard","last_synced_at":"2025-04-12T21:28:33.589Z","repository":{"id":56993380,"uuid":"49392215","full_name":"irazasyed/jwt-auth-guard","owner":"irazasyed","description":"JWT Auth Guard for Laravel and Lumen Frameworks.","archived":false,"fork":false,"pushed_at":"2019-12-03T06:58:52.000Z","size":39,"stargazers_count":320,"open_issues_count":6,"forks_count":41,"subscribers_count":11,"default_branch":"master","last_synced_at":"2025-04-04T01:07:42.911Z","etag":null,"topics":["auth","composer","composer-packages","driver","jwt","jwt-auth","jwt-auth-guard","jwt-authentication","laravel","laravel-5-package","laravel-package","lumen","middleware","packages","php"],"latest_commit_sha":null,"homepage":"https://bit.ly/jwt-auth-guard","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/irazasyed.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":".github/CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-01-11T00:28:15.000Z","updated_at":"2025-03-15T20:58:13.000Z","dependencies_parsed_at":"2022-08-21T13:20:35.054Z","dependency_job_id":null,"html_url":"https://github.com/irazasyed/jwt-auth-guard","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/irazasyed%2Fjwt-auth-guard","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/irazasyed%2Fjwt-auth-guard/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/irazasyed%2Fjwt-auth-guard/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/irazasyed%2Fjwt-auth-guard/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/irazasyed","download_url":"https://codeload.github.com/irazasyed/jwt-auth-guard/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248633982,"owners_count":21136957,"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":["auth","composer","composer-packages","driver","jwt","jwt-auth","jwt-auth-guard","jwt-authentication","laravel","laravel-5-package","laravel-package","lumen","middleware","packages","php"],"created_at":"2024-09-24T14:10:59.508Z","updated_at":"2025-04-12T21:28:33.567Z","avatar_url":"https://github.com/irazasyed.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# JWT Auth Guard\n\n[![Join PHP Chat][ico-phpchat]][link-phpchat]\n[![Chat on Telegram][ico-telegram]][link-telegram]\n[![Laravel Package][ico-laravel]][link-repo]\n[![Latest Version on Packagist][ico-version]][link-packagist]\n[![Software License][ico-license]](LICENSE.md)\n[![Total Downloads][ico-downloads]][link-downloads]\n\n\u003e JWT Auth Guard is a Laravel \u0026 Lumen Package that lets you use `jwt` as your driver for authentication guard in your application.\n\u003e \n\u003e The Guard uses `tymon/jwt-auth` package for authentication and token handling.\n\n## Requirements\n- Laravel or Lumen Installation.\n- [tymon/jwt-auth](https://github.com/tymondesigns/jwt-auth) `^1.0@dev` Package Setup and Config'd.\n\n## Pre-Installation\n\nFirst install and setup [tymon/jwt-auth](https://github.com/tymondesigns/jwt-auth) package.\n\n``` bash\n$ composer require tymon/jwt-auth:^1.0@dev\n```\n\nOnce done, config it and then install this package.\n\n## Install\n\nVia Composer\n\n``` bash\n$ composer require irazasyed/jwt-auth-guard\n```\n\n### Add the Service Provider\n\n#### Laravel\n\nOpen `config/app.php` and, to your `providers` array at the bottom, add:\n\n```php\nIrazasyed\\JwtAuthGuard\\JwtAuthGuardServiceProvider::class\n```\n\n#### Lumen\n\nOpen `bootstrap/app.php` and register the service provider:\n\n``` php\n$app-\u003eregister(Irazasyed\\JwtAuthGuard\\JwtAuthGuardServiceProvider::class);\n```\n\n## Usage\n\nOpen your `config/auth.php` config file and in place of driver under any of your guards, just add the `jwt-auth` as your driver and you're all set.\nMake sure you also set `provider` for the guard to communicate with your database.\n\n### Setup Guard Driver\n\n``` php\n// config/auth.php\n'guards' =\u003e [\n    'api' =\u003e [\n        'driver' =\u003e 'jwt-auth',\n        'provider' =\u003e 'users'\n    ],\n    \n    // ...\n],\n\n'providers' =\u003e [\n    'users' =\u003e [\n        'driver' =\u003e 'eloquent',\n        'model'  =\u003e App\\User::class,\n    ],\n],\n```\n\n### Middleware Usage\n\nMiddleware protecting the route:\n\n``` php\nRoute::get('api/content', ['middleware' =\u003e 'auth:api', 'uses' =\u003e 'ContentController@content']);\n```\n\nMiddleware protecting the controller:\n\n``` php\n\u003c?php\n\nnamespace App\\Http\\Controllers;\n\nclass ContentController extends Controller\n{\n    public function __construct() \n    {\n        $this-\u003emiddleware('auth:api');\n    }\n}\n```\n\n**Note:** The above example assumes you've setup a guard with the name `api` whose driver is `jwt-auth` in your `config/auth.php` file as explained in \"Setup Guard Driver\" section above.\n\n\u003e The following usage examples assume you've setup your default auth guard to the one which uses the `jwt-auth` driver.\n\u003e\n\u003e You can also explicitly define the guard before making calls to any of methods by just prefixing it with `Auth::guard('api')`. \n\u003e\n\u003e Example: `Auth::guard('api')-\u003euser()`\n\n### Attempt To Authenticate And Return Token\n\n``` php\n// This will attempt to authenticate the user using the credentials passed and returns a JWT Auth Token for subsequent requests.\n$token = Auth::attempt(['email' =\u003e 'user@domain.com', 'password' =\u003e '123456']);\n```\n\n### Authenticate Once By ID\n\n``` php\nif(Auth::onceUsingId(1)) {\n    // Do something with the authenticated user\n}\n```\n\n### Authenticate Once By Credentials\n\n``` php\nif(Auth::once(['email' =\u003e 'user@domain.com', 'password' =\u003e '123456'])) {\n    // Do something with the authenticated user\n}\n```\n\n### Validate Credentials\n\n``` php\nif(Auth::validate(['email' =\u003e 'user@domain.com', 'password' =\u003e '123456'])) {\n    // Credentials are valid\n}\n```\n\n### Check User is Authenticated\n\n``` php\nif(Auth::check()) {\n    // User is authenticated\n}\n```\n\n### Check User is a Guest\n\n``` php\nif(Auth::guest()) {\n    // Welcome guests!\n}\n```\n\n### Logout Authenticated User\n\n``` php\nAuth::logout(); // This will invalidate the current token and unset user/token values.\n```\n\n### Generate JWT Auth Token By ID\n   \n``` php\n$token = Auth::generateTokenById(1);\n\necho $token;\n```\n\n### Get Authenticated User\n\nOnce the user is authenticated via a middleware, You can access its details by doing:\n\n``` php\n$user = Auth::user();\n```\n\nYou can also manually access user info using the token itself:\n\n``` php\n$user = Auth::setToken('YourJWTAuthToken')-\u003euser();\n```\n\n### Get Authenticated User's ID\n\n``` php\n$userId = Auth::id();\n```\n\n### Refresh Expired Token\n\nThough it's recommended you refresh using the middlewares provided with the package,\nbut if you'd like, You can also do it manually with this method.\n\nRefresh expired token passed in request:\n\n``` php\n$token = Auth::refresh();\n```\n\nRefresh passed expired token:\n\n``` php\nAuth::setToken('ExpiredToken')-\u003erefresh();\n```\n\n### Invalidate Token\n\nInvalidate token passed in request:\n\n``` php\n$forceForever = false;\nAuth::invalidate($forceForever);\n```\n\nInvalidate token by setting one manually:\n\n``` php\n$forceForever = false;\nAuth::setToken('TokenToInvalidate')-\u003einvalidate($forceForever);\n```\n\n### Get Token\n\n``` php\n$token = Auth::getToken(); // Returns current token passed in request.\n```\n\n### Get Token Payload\n\nThis method will decode the token and return its raw payload.\n\nGet Payload for the token passed in request:\n\n``` php\n$payload = Auth::getPayload();\n```\n\nGet Payload for the given token manually:\n\n``` php\n$payload = Auth::setToken('TokenToGetPayload')-\u003egetPayload();\n```\n\n## Change log\n\nPlease see [CHANGELOG](.github/CHANGELOG.md) for more information what has changed recently.\n\n## Testing\n\n``` bash\n$ composer test\n```\n\n## Contributing\n\nPlease see [CONTRIBUTING](.github/CONTRIBUTING.md) and [CONDUCT](.github/CONDUCT.md) for details.\n\nAny issues, feedback, suggestions or questions please use issue tracker [here](https://github.com/irazasyed/jwt-auth-guard/issues).\n\n## Security\n\nIf you discover any security related issues, please email syed+gh@lukonet.com instead of using the issue tracker.\n\n## Credits\n\n- [Syed Irfaq R.][link-author]\n- [All Contributors][link-contributors]\n\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n\n[ico-phpchat]: https://img.shields.io/badge/Slack-PHP%20Chat-5c6aaa.svg?style=flat-square\u0026logo=slack\u0026labelColor=4A154B\n[ico-telegram]: https://img.shields.io/badge/@PHPChatCo-2CA5E0.svg?style=flat-square\u0026logo=telegram\u0026label=Telegram\n[ico-version]: https://img.shields.io/packagist/v/irazasyed/jwt-auth-guard.svg?style=flat-square\n[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square\n[ico-travis]: https://img.shields.io/travis/irazasyed/jwt-auth-guard/master.svg?style=flat-square\n[ico-scrutinizer]: https://img.shields.io/scrutinizer/coverage/g/irazasyed/jwt-auth-guard.svg?style=flat-square\n[ico-code-quality]: https://img.shields.io/scrutinizer/g/irazasyed/jwt-auth-guard.svg?style=flat-square\n[ico-downloads]: https://img.shields.io/packagist/dt/irazasyed/jwt-auth-guard.svg?style=flat-square\n[ico-laravel]: https://img.shields.io/badge/Laravel-5|6-FF2D20.svg?style=flat-square\u0026logo=laravel\u0026labelColor=black\u0026logoColor=white\n\n[link-phpchat]: https://phpchat.co/?ref=jwt-auth-guard\n[link-telegram]: https://t.me/PHPChatCo\n[link-repo]: https://github.com/irazasyed/jwt-auth-guard\n[link-packagist]: https://packagist.org/packages/irazasyed/jwt-auth-guard\n[link-travis]: https://travis-ci.org/irazasyed/jwt-auth-guard\n[link-scrutinizer]: https://scrutinizer-ci.com/g/irazasyed/jwt-auth-guard/code-structure\n[link-code-quality]: https://scrutinizer-ci.com/g/irazasyed/jwt-auth-guard\n[link-downloads]: https://packagist.org/packages/irazasyed/jwt-auth-guard\n[link-author]: https://github.com/irazasyed\n[link-contributors]: ../../contributors\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Firazasyed%2Fjwt-auth-guard","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Firazasyed%2Fjwt-auth-guard","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Firazasyed%2Fjwt-auth-guard/lists"}