Fork from https://github.com/damirka/yii2-jwt
JWT implementation for Yii2 Authorization process
For details see JWT official website.
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"
There is only one trait - UserTrait - which gives you 5 methods for authorization and JWT-management in User model
Your project need to be an yii2-app-advanced , and here is the guide
Set up:
<?php
$params = [
'JWT_SECRET' => 'your_secret',
'JWT_EXPIRE' => 10*24*60*60
]
<?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(),],
]
]);
}
}
<?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;
}
<?php
// $user is an User object
$token = $user->getJwt()