PHP code example of tinywan / jwt

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

    

tinywan / jwt example snippets


use Tinywan\Jwt\JwtToken;

$user = [
    'id'    => 2022,
    'name'  => 'Tinywan',
    'email' => '[email protected]',
];

$token = JwtToken::generateToken($user);
var_dump(json_encode($token));

$id = Tinywan\Jwt\JwtToken::getCurrentId();

$extend = Tinywan\Jwt\JwtToken::getExtend();

$email = Tinywan\Jwt\JwtToken::getExtendVal('email');

$refreshToken = Tinywan\Jwt\JwtToken::refreshToken();

$exp = Tinywan\Jwt\JwtToken::getTokenExp();

'is_single_device' => true,

$user = [
    'id'     => 2022,
    'name'   => 'Tinywan',
    'client' => 'MOBILE',
];
$token = Tinywan\Jwt\JwtToken::generateToken($user);
var_dump(json_encode($token));

$user = Tinywan\Jwt\JwtToken::getUser();

'user_model' => function($uid) {
    // Returns array
    return \think\facade\Db::table('resty_user')
        ->field('id,username,create_time')
        ->where('id', $uid)
        ->find();
}

'user_model' => function($uid) {
    // Returns object
    return \support\Db::table('resty_user')
        ->where('id', $uid)
        ->select('id', 'email', 'mobile', 'create_time')
        ->first();
}

$res = Tinywan\Jwt\JwtToken::clear();

// Generate WEB token
$user = [
    'id'     => 2022,
    'name'   => 'Tinywan',
    'client' => JwtToken::TOKEN_CLIENT_WEB,
];
$token = JwtToken::generateToken($user);

// Generate Mobile token
$user = [
    'id'     => 2022,
    'name'   => 'Tinywan',
    'client' => JwtToken::TOKEN_CLIENT_MOBILE,
];
$token = JwtToken::generateToken($user);

$extend = [
    'id'         => 2024,
    'access_exp' => 7200,  // 2 hours
];
$token = Tinywan\Jwt\JwtToken::generateToken($extend);