msheng / yii2-jwt
Trait for easier JWT integration
Installs: 5 692
Dependents: 0
Suggesters: 0
Security: 0
Stars: 7
Watchers: 2
Forks: 20
Type:yii2-extension
Requires
- firebase/php-jwt: ~4.0.0
- yiisoft/yii2: ~2.0.0
This package is not auto-updated.
Last update: 2025-01-04 21:30:38 UTC
README
Fork from https://github.com/damirka/yii2-jwt
yii2-jwt
JWT implementation for Yii2 Authorization process
For details see JWT official website.
Installation
To install (only master is available now) run:
composer require "msheng/yii2-jwt:~1.0.0"
Or add this line to require section of composer.json:
"msheng/yii2-jwt": "~1.0.0"
Usage
There is only one trait - UserTrait - which gives you 5 methods for authorization and JWT-management in User model
project
Your project need to be an yii2-app-advanced , and here is the guide
Set up:
In common/config/params.php
<?php $params = [ 'JWT_SECRET' => 'your_secret', 'JWT_EXPIRE' => 10*24*60*60 ]
In controller:
<?php // ... use yii\filters\auth\CompositeAuth; use yii\filters\auth\HttpBearerAuth; class BearerAuthController extends \yii\rest\ActiveController { public function behaviors() { return array_merge(parent::behaviors(), [ 'authenticator' => [ 'class' => CompositeAuth::className(), 'authMethods' => [HttpBearerAuth::className(),], ] ]); } }
In User model:
<?php // ... use yii\db\ActiveRecord; use yii\web\IdentityInterface class User extends ActiveRecord implements IdentityInterface { // Use the trait in your User model use \msheng\JWT\UserTrait; }
Get the jwt
<?php // $user is an User object $token = $user->getJwt()