PHP code example of jybtx / token-auth
1. Go to this page and download the library: Download jybtx/token-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/ */
jybtx / token-auth example snippets
'providers' => [
...
Jybtx\TokenAuth\Providers\TokenAuthServiceProvider::class,
],
'aliases' => [
...
"TokenAuth" => Jybtx\TokenAuth\Facades\TokenAuthFace::class,
]
use TokenAuth;
class TestController extends Controller
{
public function refresh()
{
$token = TokenAuth::getRefreshToken();
return $this->respondWithToken($token);
}
public function getToken()
{
$data = [
'name' => 'joe',
'age' => 18,
'sex' => 'girl',
'like' => 'sport'
];
$flag = 'user-name'; // 用户的唯一标识
$token = TokenAuth::getCreateAccessToken( $data, $flag );
return $this->respondWithToken($token);
}
protected function respondWithToken($token)
{
return response()->json([
'access_token' => $token,
'token_type' => 'bearer',
'expires_in' => gettl()
]);
}
public function me()
{
return authUser();
}
public function logout()
{
TokenAuth::TokenAddBlacklist();
return response()->json(['status'=>'success','message' => 'Successfully logged out']);
}
}
namespace Jybtx\TokenAuth\Http\Middleware;
use Jybtx\TokenAuth\JwtAuthToken;
use Illuminate\Support\Facades\Redis;
use Jybtx\TokenAuth\Support\CreateToken;
use Jybtx\TokenAuth\Support\TokenValidator;
use Jybtx\TokenAuth\Support\TokenBlackList;
use Jybtx\TokenAuth\Support\AuthenticationHeader;
abstract class BaseMiddleware
{
use TokenValidator,AuthenticationHeader,TokenBlackList,CreateToken;
/**
* [checkTokenRefreshTimeForRestApi description]
* @author jybtx
* @date 2020-05-06
* @return [type] [description]
*/
public function checkTokenRefreshTimeForRestApi()
{
return $this->verifyRefresh( getoken() );
}
/**
* [Check token value of user REST API]
* @author jybtx
* @date 2019-12-16
* @return [type] [description]
*/
public function checkTokenForRestApi()
{
return $this->getVerifyToken( getoken() );
}
/**
* Set the authentication header.
*
* @param \Illuminate\Http\Response|\Illuminate\Http\JsonResponse $response
* @param string|null $token
*
* @return \Illuminate\Http\Response|\Illuminate\Http\JsonResponse
*/
public function setAuthenticationHeaders($response, $token = null)
{
return $this->getSetAuthenticationHeader($response, $token);
}
/**
* [Check the blacklist token value of the user REST API]
* @author jybtx
* @date 2019-12-16
* @return [type] [description]
*/
public function checkTokenIsInBlacklistForApi()
{
return Redis::exists( md5( getoken() ) );
}
}
use Jybtx\TokenAuth\Http\Middleware\BaseMiddleware;
class xxxxMiddleware extends BaseMiddleware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
/**
* 验证token是否在黑名单中
*/
if ( $this->checkTokenIsInBlacklistForApi() ) return response()->json(['status'=>100,'message'=>"token 无效请重新登录!"]);
/**
* 检查token是否有效
* token在有效期内重新更新token值
* 设置响应头
*/
if ( !$this->checkTokenForRestApi() ) {
if ( $this->checkTokenRefreshTimeForRestApi() ) {
return $this->setAuthenticationHeaders($next($request));
} else {
return response()->json(['status'=>100,'message'=>"token 无效请重新登录!"]);
}
}
return $next($request);
}
}
authUser()
getoken()
gettl()
get_token_data( $string )
shell
php artisan vendor:publish --provider "Jybtx\TokenAuth\Provider\TokenAuthServiceProvider"
shell
php artisan token:generate