代码拉取完成,页面将自动刷新
<?php
require './vendor/autoload.php';
use Anker1992\JWTAuth\Facade\Auth;
use Anker1992\JWTAuth\JWTAuthException;
use Anker1992\JWTAuth\JWTAuthCode;
ini_set('display_errors', 'on');
error_reporting(-1);
// getToken();
checkToken();
// refreshToken();
// killToken();
//获取令牌
function getToken() {
$data = [
'user_id'=>12
];
try{
$res = Auth::getToken($data);
}catch (JWTAuthException $e){
echo json_encode(['error_msg'=>'加密出错']);
}
var_dump($res);exit;
}
//权限认证
function checkToken() {
$token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsImtpZCI6ImlhNkhBem14ZkVBdm95MHMifQ.eyJuYmYiOjE2MjE0MTg4ODgsImV4cCI6MTYyMjAyMzY4OCwiand0X2lkZSI6ImlhNkhBem14ZkVBdm95MHMiLCJkYXRhIjp7Im5hbWUiOiJhbmtlciJ9fQ.BuTu-WptVFm1FRYYF8K8g7XGe6-9TPy3FVjTtOqidSI';
try{
$res = Auth::check($token);
}catch (JWTAuthException $e){
//token暂时失效,请刷新令牌
if($e->getCode() === JWTAuthCode::TOKEN_EXPIRE){
echo json_encode(['error_msg'=> $e->getMessage()]);
} else if($e->getCode() === JWTAuthCode::INVALID_TOKEN){
echo json_encode(['error_msg'=>$e->getMessage()]);
}else{
echo json_encode(['error_msg'=>'登录过期,请重新登录']);
}
}
var_dump($res);
}
//刷新令牌
function refreshToken() {
$token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsImtpZCI6IjJYUllseU9Sdmg1YkJQd0QifQ.eyJuYmYiOjE2MjE0MTQwNDEsImV4cCI6MTYyMjAxODg0MSwiand0X2lkZSI6IjJYUllseU9Sdmg1YkJQd0QiLCJkYXRhIjp7Im5hbWUiOiJhbmtlciJ9fQ.KcWa839Hb1adfwgHrzbh1m_y4CHf-CQ08iWWlYZXmyY';
try{
$res = Auth::refreshToken($token);
}catch (JWTAuthException $e){
echo json_encode(['error_msg'=>'token不合法']);
}
var_dump($res);
}
//注销令牌,账号登出
function killToken(){
$token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsImtpZCI6ImlhNkhBem14ZkVBdm95MHMifQ.eyJuYmYiOjE2MjE0MTg4ODgsImV4cCI6MTYyMjAyMzY4OCwiand0X2lkZSI6ImlhNkhBem14ZkVBdm95MHMiLCJkYXRhIjp7Im5hbWUiOiJhbmtlciJ9fQ.BuTu-WptVFm1FRYYF8K8g7XGe6-9TPy3FVjTtOqidSI';
try{
Auth::killToken($token);
}catch (JWTAuthException $e){
echo json_encode(['error_msg'=>'token不合法']);
}
echo('logout success');
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。