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

ActiveYii2-extension[Authentication &amp; Authorization](/categories/authentication)

asinfotrack/yii2-jwt
====================

Yii2-jwt is a set of functionality to work with JSON Web Tokens

0.8.0(8y ago)316.1k↑900%1MITPHPPHP &gt;=5.4.0

Since Feb 27Pushed 8y ago5 watchersCompare

[ Source](https://github.com/asinfotrack/yii2-jwt)[ Packagist](https://packagist.org/packages/asinfotrack/yii2-jwt)[ RSS](/packages/asinfotrack-yii2-jwt/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (1)Dependencies (2)Versions (2)Used By (0)

Yii2-jwt
========

[](#yii2-jwt)

Yii2-jwt is a set of functionality to work with JSON Web Tokens. It is a wrapper around the JWT extension `firebase/php-jwt` which can be attached to any class as a trait. The data of tokens is encapsulated in helper classes and represented as arrays, as it is usual within Yii2-applications.

Also check out the [repository of the firebase jwt extension](https://github.com/firebase/php-jwt).

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

[](#installation)

### Basic installation

[](#basic-installation)

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

Either run

```
$ composer require asinfotrack/yii2-jwt
```

or add

```
"asinfotrack/yii2-jwt": "~0.8.0"

```

to the `require` section of your `composer.json` file.

Usage
-----

[](#usage)

Simply add the `JwtTokenTrait` to any class you wish to create tokens for. Usually this is a user class.

```
class User extends \yii\db\ActiveRecorde implements \yii\web\IdentityInterface
{

	//...

	use JwtTokenTrait;

	//...

	/**
	 * @inheritdoc
	 */
	public static function findIdentityByAccessToken($token, $type=null)
	{
		/* @var $result \asinfotrack\yii2\jwt\helpers\JwtTokenDecodingResult */

		try {
			//try decoding the token
			$result = $this->decodeJwtToken($token, Yii::$app->params['myJwtTokenSecret'], true, true);
		} catch (JwtException $e) {
			//check if token is valid but expired
			if ($e instanceof \asinfotrack\yii2\jwt\exceptions\JwtExpiredException) {
				//delete expired token from db
			}

			//return null to signal user could not be found
			return null;
		}

		//token was valid so we can extract the id
		$modelId = $result->getJti();

		//return the user model or null if not found
		return static::findOne($modelId);
	}

	//...

	/**
	 * Create a token for the current user model instance. You might want to persist
	 * the result in a token table to keep track of the tokens created.
	 *
	 * @return string the created token
	 */
	protected function createTokenForUser()
	{
		/* @var $request \asinfotrack\yii2\jwt\helpers\JwtTokenIssueRequest */

		//optional request if additional data is required (eg user roles)
		$userRoles = array_keys(Yii::$app->authManager->getRolesByUser($this->id));
		$request = new JwtIssueRequest();
		$request->setPayloadEntry('roles', $userRoles);

		return $this->createJwtToken($this->id, Yii::$app->params['myJwtTokenSecret'], $request);
	}

	//...

}
```

Changelog
---------

[](#changelog)

###### \[v0.8.0\] (work in progress)

[](#v080-work-in-progress)

- main classes in a stable condition
- further features will be added in a backwards-compatible way from here on
- all breaking changes will lead to a new minor version.

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity25

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity48

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

2996d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/32196c480a4cca0724afc5982064d2f02aa2975f66e5bef343f7f72f6cefaf08?d=identicon)[asinfotrack](/maintainers/asinfotrack)

---

Top Contributors

[![pasci84](https://avatars.githubusercontent.com/u/6659672?v=4)](https://github.com/pasci84 "pasci84 (7 commits)")

---

Tags

jwthelperauthtokenyii2

### Embed Badge

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

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

###  Alternatives

[adhocore/jwt

Ultra lightweight JSON web token (JWT) library for PHP5.5+.

3031.6M15](/packages/adhocore-jwt)[bizley/jwt

JWT integration for Yii 2

67425.3k2](/packages/bizley-jwt)[kakadu-dev/yii2-jwt-auth

Extension provide JWT auth for Yii2

105.8k](/packages/kakadu-dev-yii2-jwt-auth)[omnilight/yii2-tokens

Provides classes for token validations for Yii2 framework

105.0k](/packages/omnilight-yii2-tokens)

PHPackages © 2026

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