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

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

ngphp/jwt
=========

NGPHP/JWT is a simple library to encode and decode JSON Web Tokens (JWT) in PHP. This library is part of the NGPHP framework and was developed by Vedat Yıldırım. It provides methods to generate, validate, and decode JWT tokens, conforming to the current specifications.

1.0.0(1y ago)27MITPHPPHP ^8.0

Since Jun 13Pushed 1y ago2 watchersCompare

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

READMEChangelog (1)DependenciesVersions (2)Used By (0)

JWT
===

[](#jwt)

NGPHP/JWT is a simple library to encode and decode JSON Web Tokens (JWT) in PHP. This library is part of the NGPHP framework and was developed by Vedat Yıldırım. It provides methods to generate, validate, and decode JWT tokens, conforming to the current specifications.

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

[](#installation)

You can install this package via Composer:

```
composer install ngphp/jwt
```

Example
-------

[](#example)

file: vendor/ngphp/src/example.php url:

Usage
-----

[](#usage)

### Generating a JWT

[](#generating-a-jwt)

```
// without composer: require 'JWT.php';

// with composer
require 'vendor/autoload.php';

use ngphp\JWT;

// Initialize the JWT handler with a secret key
$secret = 'your_secret_key';
$jwt = new JWT($secret);

// Example payload
$payload = [
    'user_id' => 1,
    'username' => 'john.doe',
    'email' => 'john.doe@example.com',
    'role' => 'admin',
    'iat' => time(),
    'exp' => time() + 3600 // 1 hour expiration
];

// Generate a JWT
$token = $jwt->generate($payload);
echo "Generated Token: " . $token . "\n";

```

### Authenticating a JWT

[](#authenticating-a-jwt)

```
// Example headers containing the JWT
$headers = [
    'Authorization' => 'Bearer ' . $token
];

// Authenticate the user using the token from the headers
try {
    if ($jwt->authenticate($headers)) {
        $user = JWT::getUser();
        echo "Authenticated User: \n";
        print_r($user);
    }
} catch (\Exception $e) {
    echo "Authentication failed: " . $e->getMessage() . "\n";
}

```

### Decoding a JWT

[](#decoding-a-jwt)

```
// Decode the token directly
try {
    $decoded = $jwt->decode($token);
    echo "Decoded Token: \n";
    print_r($decoded);
} catch (\Exception $e) {
    echo "Decoding failed: " . $e->getMessage() . "\n";
}

```

### Example

[](#example-1)

Here's a more detailed example that demonstrates generating, storing, authenticating, and decoding a JWT.

```
require 'vendor/autoload.php';

use leanphp\jwt\JWT;

// Initialize the JWT handler with a secret key
$secret = 'your_secret_key';
$jwt = new JWT($secret);

// Example payload
$payload = [
    'user_id' => 1,
    'username' => 'john.doe',
    'email' => 'john.doe@example.com',
    'role' => 'admin',
    'iat' => time(),
    'exp' => time() + 3600 // 1 hour expiration
];

// Generate a JWT
$token = $jwt->generate($payload);
echo "Generated Token: " . $token . "\n\n";

// Simulate storing the token in a database
echo "Storing the token in the database...\n\n";
// Example of database storage logic (not implemented here)
// storeTokenInDatabase($user_id, $token);

// Example headers containing the JWT
$headers = [
    'Authorization' => 'Bearer ' . $token
];

// Authenticate the user using the token from the headers
echo "Authenticating User...\n";
try {
    if ($jwt->authenticate($headers)) {
        $user = JWT::getUser();
        echo "Authenticated User: \n";
        print_r($user);
        echo "\n";
    }
} catch (\Exception $e) {
    echo "Authentication failed: " . $e->getMessage() . "\n";
}

// Decode the token directly
echo "Decoding the Token...\n";
try {
    $decoded = $jwt->decode($token);
    echo "Decoded Token: \n";
    print_r($decoded);
    echo "\n";
} catch (\Exception $e) {
    echo "Decoding failed: " . $e->getMessage() . "\n";
}

// Example output separation
echo "\n--------------------------\n";
echo "Example 1: Token Generation\n";
echo "Generated Token: " . $token . "\n";

echo "\n--------------------------\n";
echo "Example 2: Token Authentication\n";
try {
    if ($jwt->authenticate($headers)) {
        $user = JWT::getUser();
        echo "Authenticated User: \n";
        print_r($user);
        echo "\n";
    }
} catch (\Exception $e) {
    echo "Authentication failed: " . $e->getMessage() . "\n";
}

echo "\n--------------------------\n";
echo "Example 3: Token Decoding\n";
try {
    $decoded = $jwt->decode($token);
    echo "Decoded Token: \n";
    print_r($decoded);
    echo "\n";
} catch (\Exception $e) {
    echo "Decoding failed: " . $e->getMessage() . "\n";
}

```

License This project is licensed under the MIT License. See the LICENSE file for more details.

Acknowledgements This library is part of the LeanPHP framework and was developed by Vedat Yıldırım.

Contributing If you find any issues or have suggestions for improvements, feel free to open an issue or submit a pull request on GitHub.

This `README.md` provides an overview of the JWT library, including installation instructions, usage examples, and detailed explanations of each method. It also includes a complete example demonstrating how to generate, authenticate, and decode JWT tokens. The repository name `php-jwt` is concise and SEO-friendly, making it easier for users to find your project when searching for JWT solutions in PHP.

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance32

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity45

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

Unknown

Total

1

Last Release

704d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/463566415777c7e574a7aac68aade17a1be31190374aa6387b15da84407a31e1?d=identicon)[vedatyildirim](/maintainers/vedatyildirim)

---

Top Contributors

[![vedatyildirim](https://avatars.githubusercontent.com/u/13311826?v=4)](https://github.com/vedatyildirim "vedatyildirim (5 commits)")

---

Tags

jwtjwt-authjwt-authenticationjwt-tokenphpjwtJSON Web Token

### Embed Badge

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

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

###  Alternatives

[auth0/auth0-php

PHP SDK for Auth0 Authentication and Management APIs.

40820.2M68](/packages/auth0-auth0-php)[griffinledingham/php-apple-signin

A simple library to decode and parse Apple Sign In client tokens.

2011.9M1](/packages/griffinledingham-php-apple-signin)

PHPackages © 2026

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