优化代码

main
gzydong 2021-05-23 17:04:20 +08:00
parent 60ea1688b7
commit b65d427011
4 changed files with 35 additions and 11 deletions

View File

@ -54,7 +54,7 @@ class AuthController extends CController
} }
try { try {
$token = auth('jwt')->login($userInfo); $token = $this->guard()->login($userInfo);
} catch (\Exception $exception) { } catch (\Exception $exception) {
return $this->response->error('登录异常,请稍后再试!'); return $this->response->error('登录异常,请稍后再试!');
} }
@ -62,7 +62,7 @@ class AuthController extends CController
return $this->response->success([ return $this->response->success([
'authorize' => [ 'authorize' => [
'access_token' => $token, 'access_token' => $token,
'expires_in' => auth('jwt')->getJwtManager()->getTtl() 'expires_in' => $this->guard()->getJwtManager()->getTtl()
], ],
'user_info' => [ 'user_info' => [
'nickname' => $userInfo->nickname, 'nickname' => $userInfo->nickname,
@ -71,7 +71,7 @@ class AuthController extends CController
'motto' => $userInfo->motto, 'motto' => $userInfo->motto,
'email' => $userInfo->email, 'email' => $userInfo->email,
] ]
]); ], '账号登录成功...');
} }
/** /**
@ -80,9 +80,9 @@ class AuthController extends CController
*/ */
public function logout() public function logout()
{ {
auth('jwt')->check() && auth('jwt')->logout(); $this->guard()->check() && $this->guard()->logout();
return $this->response->success([], 'Successfully logged out'); return $this->response->success([], '退出登录成功...');
} }
/** /**
@ -154,14 +154,14 @@ class AuthController extends CController
*/ */
public function refresh() public function refresh()
{ {
if (auth('jwt')->guest()) { if ($this->guard()->guest()) {
return $this->response->fail('登录 token 刷新失败!'); return $this->response->fail('登录 token 刷新失败!');
} }
return $this->response->success([ return $this->response->success([
'authorize' => [ 'authorize' => [
'token' => auth('jwt')->refresh(), 'token' => $this->guard()->refresh(),
'expire' => auth('jwt')->getJwtManager()->getTtl() 'expire' => $this->guard()->getJwtManager()->getTtl()
] ]
]); ]);
} }

View File

@ -34,8 +34,18 @@ class CController extends AbstractController
*/ */
public function uid() public function uid()
{ {
$guard = auth('jwt'); $guard = $this->guard();
return $guard->check() ? $guard->user()->getId() : 0; return $guard->check() ? $guard->user()->getId() : 0;
} }
/**
* 获取授权守卫
*
* @return mixed|\Qbhy\HyperfAuth\AuthGuard|\Qbhy\HyperfAuth\AuthManager
*/
public function guard()
{
return auth('jwt');
}
} }

View File

@ -34,6 +34,13 @@ class JWTAuthMiddleware implements MiddlewareInterface
*/ */
protected $response; protected $response;
/**
* 授权验证守卫
*
* @var string
*/
private $guard = 'jwt';
public function __construct(HttpResponse $response, RequestInterface $request) public function __construct(HttpResponse $response, RequestInterface $request)
{ {
$this->response = $response; $this->response = $response;
@ -42,7 +49,7 @@ class JWTAuthMiddleware implements MiddlewareInterface
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{ {
if (auth('jwt')->guest()) { if (auth($this->guard)->guest()) {
return $this->response->withStatus(401)->json([ return $this->response->withStatus(401)->json([
'code' => 401, 'code' => 401,
'message' => 'Token authentication does not pass !', 'message' => 'Token authentication does not pass !',

View File

@ -30,6 +30,13 @@ class WebSocketAuthMiddleware implements MiddlewareInterface
*/ */
protected $container; protected $container;
/**
* 授权验证守卫
*
* @var string
*/
private $guard = 'jwt';
public function __construct(ContainerInterface $container) public function __construct(ContainerInterface $container)
{ {
$this->container = $container; $this->container = $container;
@ -38,7 +45,7 @@ class WebSocketAuthMiddleware implements MiddlewareInterface
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{ {
// 授权验证拦截握手请求并实现权限检查 // 授权验证拦截握手请求并实现权限检查
if (auth('jwt')->guest()) { if (auth($this->guard)->guest()) {
return $this->container->get(\Hyperf\HttpServer\Contract\ResponseInterface::class)->raw('Forbidden'); return $this->container->get(\Hyperf\HttpServer\Contract\ResponseInterface::class)->raw('Forbidden');
} }