PHP code example of tegic / hyperf-jwt

1. Go to this page and download the library: Download tegic/hyperf-jwt library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tegic / hyperf-jwt example snippets




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();

    }
}