承接 tahajaiti/jwt 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

tahajaiti/jwt

最新稳定版本:1.2

Composer 安装命令:

composer require tahajaiti/jwt

包简介

A laravel package for JWT Authentication

README 文档

README

A Laravel package for implementing JSON Web Token (JWT) authentication with a robust service layer, middleware, traits, and commands.

Features

  • JWT token generation and validation
  • Middleware for protected routes
  • Trait for easy token creation in models
  • Command-line setup tool
  • Facade for convenient access
  • Comprehensive error handling

Requirements

  • PHP >= 8.2
  • Laravel >= 11.x/ 12.x
  • Composer

Installation

Install the package via Composer:

composer require tahajaiti/jwt

Run the setup command to configure the package:

php artisan jwt:setup
  • This will:
  • Create or update your .env file with JWT variables
  • Publish the configuration file to config/jwt.php
  • Clear configuration and cache

Configuration

  • The package publishes a configuration file at config/jwt.php. Default values are set in the .env file:
JWT_SECRET=your-secret
JWT_ALGO=HS256
JWT_TTL=3600
  • You can customize these in either the .env file or config/jwt.php:
return [
    'secret' => env('JWT_SECRET', 'fallback-secret'),
    'algo' => env('JWT_ALGO', 'HS256'),
    'ttl' => env('JWT_TTL', 3600),
];

Usage

Service Provider

The package automatically registers its service provider. If you need to customize it, add to bootstrap/providers.php:

'providers' => [
    // ...
    Kyojin\JWT\Providers\JWTServiceProvider::class,
],

Generating Tokens

Using the Trait

  • Add the HasJWT trait to your User model:
use Kyojin\JWT\Traits\HasJWT;

class User extends Authenticatable
{
    use HasJWT;

    // Optional: Customize payload
    public function payload(): array
    {
        return [
            'role' => $this->role
        ];
    }
}
  • Create a token:
$user = User::find(1);
$token = $user->createToken(); // New token

Using the Facade

use Kyojin\JWT\Facades\JWT;

$payload = ['sub' => 1, 'role' => 'admin'];
$token = JWT::encode($payload);

Middleware

Protect routes with the JWT authentication middleware:

// In routes/api.php
Route::middleware('jwt')->group(function () {
    Route::get('/test', function () {
        return response()->json(['message' => 'Authenticated']);
    });
});

Requests must include an Authorization header:

Authorization: Bearer your-jwt-token

Or optionally you can send the token through Http Cookies as 'jwt_token'

Validating Tokens

The default middleware automatically binds the user to the Auth facade

use Kyojin\JWT\Facades\JWT;

$isValid = JWT::validate($token); // Returns boolean
$payload = JWT::decode($token);   // Returns array or throws exception
$user = Auth::user();            // Returns the user based on the validated token 
$user = JWT::user();            // (Optional method for retrieving the user)

Error Handling

The package throws two main exceptions:

  • TokenNotFoundException: When no token is provided (401)
  • InvalidTokenException: When token is invalid or expired (401)

Handle them in your application exception handler:

use Kyojin\JWT\Exceptions\InvalidTokenException;
use Kyojin\JWT\Exceptions\TokenNotFoundException;

public function render($request, Throwable $exception)
{
    if ($exception instanceof TokenNotFoundException || $exception instanceof InvalidTokenException) {
        return response()->json(['error' => $exception->getMessage()], 401);
    }
}

Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/name)
  3. Commit your changes (git commit -m 'Add new feature')
  4. Push to the branch (git push origin feature/name)
  5. Open a Pull Request

License

This package is open source do whatever.

统计信息

  • 总下载量: 88
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-03-29