tegic/hyperf-jwt

jwt-auth Component

v3.0.1 2022-05-26 02:40 UTC

This package is auto-updated.

Last update: 2022-06-26 02:52:05 UTC


README

composer require tegic/hyperf-jwt

Usage

<?php

declare(strict_types=1);
namespace App\Controller;


use Hyperf\Di\Annotation\Inject;
use Tegic\JWTAuth\JWT;

class IndexController extends AbstractController
{

    /**
     * @Inject()
     * @var JWT
     */
    protected $jwt;

    /**
     * 生成token
     * @return \Lcobucci\JWT\Token|string
     * @throws \Psr\SimpleCache\InvalidArgumentException
     */
    public function index()
    {
        $data = [
            'user_id' => 1,
            'platform' => 1
        ];
        $token = $this->jwt->getToken($data);
        return $token;
    }

    /**
     * 检测及解析token
     * @return array
     * @throws \Psr\SimpleCache\InvalidArgumentException
     * @throws \Throwable
     */
    public function user()
    {
        $result =  $this->jwt->getParserData();
        $this->jwt->checkToken();
        $result['iat'] = date('Y-m-d H:i:s',$result['iat']);
        $result['nbf'] = date('Y-m-d H:i:s',$result['nbf']);
        $result['exp'] = date('Y-m-d H:i:s',$result['exp']);
        return $result;
    }

    /**
     * 刷新token
     * @return \Lcobucci\JWT\Token|string
     * @throws \Psr\SimpleCache\InvalidArgumentException
     */
    public function refreshToken()
    {
        return $this->jwt->refreshToken();

    }
}

Exception

JWTException token 不合法

TokenBackException token 在黑名单内

TokenValidException token 过期