百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
J

jwt-auth-guard

> 后端框架
开源

适用于 Laravel 和 Lumen 框架的 JWT Auth Guard。

318 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

适用于 Laravel 和 Lumen 框架的 JWT Auth Guard。

JWT Auth Guard

Join PHP Chat Chat on Telegram Laravel Package Latest Version on Packagist Software License Total Downloads

JWT Auth Guard is a Laravel & Lumen Package that lets you use jwt as your driver for authentication guard in your application.

The Guard uses tymon/jwt-auth package for authentication and token handling.

Requirements

  • Laravel or Lumen Installation.
  • tymon/jwt-auth ^1.0@dev Package Setup and Config'd.

Pre-Installation

First install and setup tymon/jwt-auth package.

bash
$ composer require tymon/jwt-auth:^1.0@dev

Once done, config it and then install this package.

Install

Via Composer

bash
$ composer require irazasyed/jwt-auth-guard

Add the Service Provider

Laravel

Open config/app.php and, to your providers array at the bottom, add:

php
Irazasyed\JwtAuthGuard\JwtAuthGuardServiceProvider::class

Lumen

Open bootstrap/app.php and register the service provider:

php
$app->register(Irazasyed\JwtAuthGuard\JwtAuthGuardServiceProvider::class);

Usage

Open 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. Make sure you also set provider for the guard to communicate with your database.

Setup Guard Driver

php
// config/auth.php
'guards' => [
    'api' => [
        'driver' => 'jwt-auth',
        'provider' => 'users'
    ],
    
    // ...
],

'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model'  => App\User::class,
    ],
],

Middleware Usage

Middleware protecting the route:

php
Route::get('api/content', ['middleware' => 'auth:api', 'uses' => 'ContentController@content']);

Middleware protecting the controller:

php
middleware('auth:api');
    }
}

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.

The following usage examples assume you've setup your default auth guard to the one which uses the jwt-auth driver.

You can also explicitly define the guard before making calls to any of methods by just prefixing it with Auth::guard('api').

Example: Auth::guard('api')->user()

Attempt To Authenticate And Return Token

php
// This will attempt to authenticate the user using the credentials passed and returns a JWT Auth Token for subsequent requests.
$token = Auth::attempt(['email' => '[email protected]', 'password' => '123456']);

Authenticate Once By ID

php
if(Auth::onceUsingId(1)) {
    // Do something with the authenticated user
}

Authenticate Once By Credentials

php
if(Auth::once(['email' => '[email protected]', 'password' => '123456'])) {
    // Do something with the authenticated user
}

Validate Credentials

php
if(Auth::validate(['email' => '[email protected]', 'password' => '123456'])) {
    // Credentials are valid
}

Check User is Authenticated

php
if(Auth::check()) {
    // User is authenticated
}

Check User is a Guest

php
if(Auth::guest()) {
    // Welcome guests!
}

Logout Authenticated User

php
Auth::logout(); // This will invalidate the current token and unset user/token values.

Generate JWT Auth Token By ID

php
$token = Auth::generateTokenById(1);

echo $token;

Get Authenticated User

Once the user is authenticated via a middleware, You can access its details by doing:

php
$user = Auth::user();

You can also manually access user info using the token itself:

php
$user = Auth::setToken('YourJWTAuthToken')->user();

Get Authenticated User's ID

php
$userId = Auth::id();

Refresh Expired Token

Though it's recommended you refresh using the middlewares provided with the package, but if you'd like, You can also do it manually with this method.

Refresh expired token passed in request:

php
$token = Auth::refresh();

Refresh passed expired token:

php
Auth::setToken('ExpiredToken')->refresh();

Invalidate Token

Invalidate token passed in request:

php
$forceForever = false;
Auth::invalidate($forceForever);

Invalidate token by setting one manually:

php
$forceForever = false;
Auth::setToken('TokenToInvalidate')->invalidate($forceForever);

Get Token

php
$token = Auth::getToken(); // Returns current token passed in request.

Get Token Payload

This method will decode the token and return its raw payload.

Get Payload for the token passed in request:

php
$payload = Auth::getPayload();

Get Payload for the given token manually:

php
$payload = Auth::setToken('TokenToGetPayload')->getPayload();

Change log

Please see CHANGELOG for more information what has changed recently.

Testing

bash
$ composer test

Contributing

Please see CONTRIBUTING and CONDUCT for details.

Any issues, feedback, suggestions or questions please use issue tracker here.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

  • Syed Irfaq R.
  • All Contributors

License

The MIT License (MIT). Please see License File for more information.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

PHPauthcomposercomposer-packagesdriver

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类后端框架
定价开源

> 相关工具

N
Node.js
基于 V8 的 JavaScript 运行时
D
Django
Python 高级 Web 框架
S
Spring Boot
Java 生态主流微服务框架