PHP code example of zxyfaxcn / jwt-auth

1. Go to this page and download the library: Download zxyfaxcn/jwt-auth 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/ */

    

zxyfaxcn / jwt-auth example snippets

shell
php bin/hyperf.php vendor:publish zxyfaxcn/jwt-auth
shell


namespace App\Controller;
use JwtAuth\Jwt;
class IndexController extends Controller
{
    # 模拟登录,获取token
    public function login(Jwt $jwt)
    {
        #用法1: 传入对象

        $jwtData = new JwtBuilder();
        $jwtData->setIssuer('api');
        $jwtData->setAudience('xxx');
        #... 设置更多token属性

        #... 设置data数据
        $jwtData->setJwtData(['uid' => 123, 'type' => 1111, 'group' => 1]);

        #返回 JwtBuilder对象
        $tokenObj = $jwt->createToken($jwtData);

        #获取生成的token 
        $tokenObj->getToken();  


        #用法2: 传入数组 

        #初始化JwtBuilder对象
        $tokenObj = $jwt->createToken(['uid' => $id, 'type' => $type, 'group' => $group]);

        #获取生成的token 
        $tokenObj->getToken();  

        #获取刷新token 传入数组  第一个参数为数据,第二个参数为类型,默认是access 可以定义为 refersh 或者其他类型自定义
        #返回 JwtBuilder对象  
        $tokenObj = $jwt->createToken(['uid' => $id, 'type' => $type, 'group' => $group], Jwt::SCOPE_REFRESH);

        #获取生成的token 
        $tokenObj->getToken();  

        return $tokenObj->getToken();
    }
}