PHPackages                             tahajaiti/jwt - 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. tahajaiti/jwt

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

tahajaiti/jwt
=============

A laravel package for JWT Authentication

1.2(1y ago)192MITPHPPHP ^8.2

Since Mar 29Pushed 1y ago1 watchersCompare

[ Source](https://github.com/tahajaiti/jwt)[ Packagist](https://packagist.org/packages/tahajaiti/jwt)[ RSS](/packages/tahajaiti-jwt/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (10)Versions (19)Used By (0)

Kyojin JWT Package
==================

[](#kyojin-jwt-package)

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

Features
--------

[](#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
------------

[](#requirements)

- PHP &gt;= 8.2
- Laravel &gt;= 11.x/ 12.x
- Composer

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

[](#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
-------------

[](#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
-----

[](#usage)

### Service Provider

[](#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

[](#generating-tokens)

#### Using the Trait

[](#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

[](#using-the-facade)

```
use Kyojin\JWT\Facades\JWT;

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

### Middleware

[](#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

[](#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
--------------

[](#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
------------

[](#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
-------

[](#license)

This package is open source do whatever.

###  Health Score

34

—

LowBetter than 77% of packages

Maintenance46

Moderate activity, may be stable

Popularity13

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 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 ~0 days

Total

18

Last Release

403d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/07e0d603d2172e586fa52fb8a4ba222f6d64d8c3fe802ba15d9896e4c777ec26?d=identicon)[tahajaiti](/maintainers/tahajaiti)

---

Top Contributors

[![tahajaiti](https://avatars.githubusercontent.com/u/182523740?v=4)](https://github.com/tahajaiti "tahajaiti (36 commits)")

### Embed Badge

![Health badge](/badges/tahajaiti-jwt/health.svg)

```
[![Health](https://phpackages.com/badges/tahajaiti-jwt/health.svg)](https://phpackages.com/packages/tahajaiti-jwt)
```

###  Alternatives

[tymon/jwt-auth

JSON Web Token Authentication for Laravel and Lumen

11.5k49.1M350](/packages/tymon-jwt-auth)[php-open-source-saver/jwt-auth

JSON Web Token Authentication for Laravel and Lumen

8359.8M53](/packages/php-open-source-saver-jwt-auth)[laravel/passport

Laravel Passport provides OAuth2 server support to Laravel.

3.4k85.0M532](/packages/laravel-passport)[laravel/pulse

Laravel Pulse is a real-time application performance monitoring tool and dashboard for your Laravel application.

1.7k12.1M99](/packages/laravel-pulse)[roots/acorn

Framework for Roots WordPress projects built with Laravel components.

9682.1M97](/packages/roots-acorn)[illuminate/auth

The Illuminate Auth package.

9327.3M1.0k](/packages/illuminate-auth)

PHPackages © 2026

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