devmastersbv/yii2-jwt

Trait for easier JWT integration

Installs: 205

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 2

Forks: 20

Type:yii2-extension

0.2.4 2016-11-03 13:24 UTC

This package is not auto-updated.

Last update: 2024-05-06 12:23:32 UTC


README

JWT implementation for Yii2 Authorization process

For details see JWT official website.

Installation

To install (only master is available now) run:

    composer require "damirka/yii2-jwt:v0.2.1"

Or add this line to require section of composer.json:

    "damirka/yii2-jwt": "v0.2.1"

Usage

There is only one trait - UserTrait - which gives you 5 methods for authorization and JWT-management in User model

Set up:

In controller:

<?php

// ...

use yii\filters\auth\HttpBearerAuth;

class BearerAuthController extends \yii\rest\ActiveController
{
    public function behaviors()
    {
        return array_merge(parent::behaviors(), [
            'bearerAuth' => [
                'class' => 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 \damirka\JWT\UserTrait;

    // Override this method
    protected static function getSecretKey()
    {
        return 'someSecretKey';
    }

    // And this one if you wish
    protected static function getHeaderToken()
    {
        return [];
    }

    // ...
}