PHP code example of ericjank / auth
1. Go to this page and download the library: Download ericjank/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/ */
ericjank / auth example snippets shell
php bin/hyperf.php jwt:publish --config
shell
return [
'http' => [
Phper666\JwtAuth\Middleware\JwtAuthMiddleware:class
],
];
shell
Router::addGroup('/v1', function () {
Router::get('/data', 'App\Controller\IndexController@getData');
}, ['middleware' => [Phper666\JwtAuth\Middleware\JwtAuthMiddleware::class]]);
shell
namespace App\Controller;
use \Phper666\JwtAuth\Jwt;
class IndexController extends Controller
{
# 模拟登录,获取token
public function login(Jwt $jwt)
{
$username = $this->request->input('username');
$password = $this->request->input('password');
if ($username && $password) {
$userData = [
'uid' => 1,
'username' => 'xx',
];
$token = (string)$jwt->getToken($userData);
return $this->response->json(['code' => 0, 'msg' => '获取token成功', 'data' => ['token' => $token]]);
}
return $this->response->json(['code' => 0, 'msg' => '登录失败', 'data' => []]);
}
# http头部必须携带token才能访问的路由
public function getData()
{
return $this->response->json(['code' => 0, 'msg' => 'success', 'data' => ['a' => 1]]);
}
}
shell
# 登录
Router::post('/login', 'App\Controller\IndexController@login');
# 获取数据
Router::addGroup('/v1', function () {
Router::get('/data', 'App\Controller\IndexController@getData');
}, ['middleware' => [Phper666\JwtAuth\Middleware\JwtAuthMiddleware::class]]);
shell
declare(strict_types=1);
namespace App\Controller;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\AutoController;
use \Phper666\JwtAuth\Jwt;
/**
* @AutoController()
* Class IndexController
* @package App\Controller
*/
class IndexController extends AbstractController
{
/**
* @Inject()
* @var Jwt
*/
protected $jwt;
# 模拟登录
public function login()
{
$username = $this->request->input('username');
$password = $this->request->input('password');
if ($username && $password) {
$userData = [
'uid' => 1, // 如果使用单点登录,必须存在配置文件中的sso_key的值,一般设置为用户的id
'username' => 'xx',
];
$token = $this->jwt->getToken($userData);
$data = [
'code' => 0,
'msg' => 'success',
'data' => [
'token' => (string)$token,
'exp' => $this->jwt->getTTL(),
]
];
return $this->response->json($data);
}
return $this->response->json(['code' => 0, 'msg' => '登录失败', 'data' => []]);
}
# 刷新token,http头部必须携带token才能访问的路由
public function refreshToken()
{
$token = $this->jwt->refreshToken();
$data = [
'code' => 0,
'msg' => 'success',
'data' => [
'token' => (string)$token,
'exp' => $this->jwt->getTTL(),
]
];
return $this->response->json($data);
}
# 注销token,http头部必须携带token才能访问的路由
public function logout()
{
$this->jwt->logout();
return true;
}
# http头部必须携带token才能访问的路由
public function getData()
{
$data = [
'code' => 0,
'msg' => 'success',
'data' => [
'cache_time' => $this->jwt->getTokenDynamicCacheTime() // 获取token的有效时间,动态的
]
];
return $this->response->json($data);
}
public function index()
{
$user = $this->request->input('user', 'Hyperf');
$method = $this->request->getMethod();
return [
'method' => $method,
'message' => "Hello {$user}.",
];
}
}