xzag/yii3-jwt

Trait for easier JWT integration

Installs: 6

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 2

Forks: 20

Type:yii3-extension

3.0.0 2019-05-06 18:12 UTC

This package is auto-updated.

Last update: 2024-04-07 06:44:01 UTC


README

JWT implementation for Yii3

For details see JWT official website.

Installation

To install run:

    composer require "xzag/yii3-jwt: ~3.0"

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

    "xzag/yii3-jwt": "~3.0"

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\web\filters\auth\HttpBearerAuth;
use Yiisoft\Yii\Rest\ActiveController;

class BearerAuthController extends ActiveController
{
    public function behaviors()
    {
        return array_merge(parent::behaviors(), [
            'bearerAuth' => [
                'class' => HttpBearerAuth::class
            ]
        ]);
    }
}

In User model:

<?php

// ...

use yii\activerecord\ActiveRecord;
use yii\web\IdentityInterface;

class User extends ActiveRecord implements IdentityInterface
{
    // Use the trait in your User model
    use xzag\JWT\UserTrait;

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

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

    // ...
}