JWT为生成登录token和验证token
内置方法总览
获取token
public function getToken(int $id, string $type, array $params = []): array
解析token
public function parseToken(string $jwt): array
验证token
public function verifyToken()
获取token并放入令牌桶
public function createToken(int $id, string $type, array $params = [])
使用方法
获取登录token
use crmeb\utils\JwtAuth;
$jwt = app()->make(JwtAuth::class);
$uid = 10;
$type = 'user';
$token = $jwt->createToken($uid, $type);
var_dump($token);
解析token提取uid
use crmeb\utils\JwtAuth;
use Firebase\JWT\ExpiredException;
$jwt = app()->make(JwtAuth::class);
$token = 'token';
//解析token
[$uid, $type] = $jwt->parseToken($token);
//验证token是否过期
try {
$jwt->verifyToken();
} catch (ExpiredException $e) {
//可以做过期的逻辑处理
} catch (\Throwable $e) {
//其他错误处理
}
//没有任何错误返回uid
return $uid;
Was this helpful?
0 / 0