PHP code example of yaobiao / think-jwt

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

    

yaobiao / think-jwt example snippets




return [
    'default' => 'admin',
    'apps' => [
        'admin' => [
            'token' => [
                'uniqidKey'    => 'uid',
                'signerKey'    => '',
                'notBefore'    => 0,
                'expiresAt'    => 3600,
                'refreshTTL'   => 7200,
                'signer'       => 'Lcobucci\JWT\Signer\Hmac\Sha256',
                'type'         => 'Header',
                'refresh'      => 50001,
                'relogin'      => 50002,
                'iss'          => '',
                'aud'          => '',
                'automaticRenewal' => false,
            ],
            'user' => [
                'bind' => false,
                'model'  => '',
            ]
        ]
    ],
    'manager' => [
        // 缓存前缀
        'prefix' => 'jwt',
        // 黑名单缓存名
        'blacklist' => 'blacklist',
        // 白名单缓存名
        'whitelist' => 'whitelist'
    ]
];


use xiaodi\JWTAuth\Facade\Jwt;

public function login()
{
    //...登录判断逻辑

    // 默认应用
    return json([
        'token' => Jwt::token(['uid' => 1]),
        'token_type' => Jwt::type(),
        'expires_in' => Jwt::ttl(),
        'refresh_in' => Jwt::refreshTTL()
    ]);
    
    // 指定应用
    return json([
        'token' => Jwt::store('wechat')->token(['uid' => 1]),
        'token_type' => Jwt::type(),
        'expires_in' => Jwt::ttl(),
        'refresh_in' => Jwt::refreshTTL()
    ]);
}

use xiaodi\JWTAuth\Facade\Jwt;
use xiaodi\JWTAuth\Exception\HasLoggedException;
use xiaodi\JWTAuth\Exception\TokenAlreadyEexpired;

class User {

    public function test()
    {
        try {
            // 默认应用
            Jwt::verify($token);
            
            // 指定应用
            // Jwt::store('wechat')->verify($token);
        } catch (HasLoggedException $e) {
            // 已在其它终端登录
        } catch (TokenAlreadyEexpired $e) {
            // Token已过期
        }
        
        // 验证成功
        // 如 开启用户注入功能 可获取当前用户信息
        dump(Jwt::user());
    }
}


use xiaodi\JWTAuth\Middleware\Jwt;

// 默认应用
Route::get('/hello', 'index/index')->middleware(Jwt::class);

// 指定应用
Route::get('/hello', 'index/index')->middleware(Jwt::class, 'wechat');

# config/jwt.php



return [

    'apps' => [
        'admin' => [
            'token' => [
                // ...其它配置
                'type' => 'Header',
                // 'type' => 'Cookie',
                // 'type' => 'Url',
                // 支持多种方式获取
                // 'type' => 'Header|Url',
            ]
        ]
    ]
    
];


$store = 'wechat';

app('jwt.manager')->resetStoreWhiteToken($store);
sh
$ php think jwt:make