diff --git a/app/Controller/Api/V1/AuthController.php b/app/Controller/Api/V1/AuthController.php index 721f49c..1765aed 100644 --- a/app/Controller/Api/V1/AuthController.php +++ b/app/Controller/Api/V1/AuthController.php @@ -54,7 +54,7 @@ class AuthController extends CController } try { - $token = auth('jwt')->login($userInfo); + $token = $this->guard()->login($userInfo); } catch (\Exception $exception) { return $this->response->error('登录异常,请稍后再试!'); } @@ -62,7 +62,7 @@ class AuthController extends CController return $this->response->success([ 'authorize' => [ 'access_token' => $token, - 'expires_in' => auth('jwt')->getJwtManager()->getTtl() + 'expires_in' => $this->guard()->getJwtManager()->getTtl() ], 'user_info' => [ 'nickname' => $userInfo->nickname, @@ -71,7 +71,7 @@ class AuthController extends CController 'motto' => $userInfo->motto, 'email' => $userInfo->email, ] - ]); + ], '账号登录成功...'); } /** @@ -80,9 +80,9 @@ class AuthController extends CController */ 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() { - if (auth('jwt')->guest()) { + if ($this->guard()->guest()) { return $this->response->fail('登录 token 刷新失败!'); } return $this->response->success([ 'authorize' => [ - 'token' => auth('jwt')->refresh(), - 'expire' => auth('jwt')->getJwtManager()->getTtl() + 'token' => $this->guard()->refresh(), + 'expire' => $this->guard()->getJwtManager()->getTtl() ] ]); } diff --git a/app/Controller/Api/V1/CController.php b/app/Controller/Api/V1/CController.php index 7dc4510..4bf0828 100644 --- a/app/Controller/Api/V1/CController.php +++ b/app/Controller/Api/V1/CController.php @@ -34,8 +34,18 @@ class CController extends AbstractController */ public function uid() { - $guard = auth('jwt'); + $guard = $this->guard(); return $guard->check() ? $guard->user()->getId() : 0; } + + /** + * 获取授权守卫 + * + * @return mixed|\Qbhy\HyperfAuth\AuthGuard|\Qbhy\HyperfAuth\AuthManager + */ + public function guard() + { + return auth('jwt'); + } } diff --git a/app/Middleware/JWTAuthMiddleware.php b/app/Middleware/JWTAuthMiddleware.php index a405de3..2d67f64 100644 --- a/app/Middleware/JWTAuthMiddleware.php +++ b/app/Middleware/JWTAuthMiddleware.php @@ -34,6 +34,13 @@ class JWTAuthMiddleware implements MiddlewareInterface */ protected $response; + /** + * 授权验证守卫 + * + * @var string + */ + private $guard = 'jwt'; + public function __construct(HttpResponse $response, RequestInterface $request) { $this->response = $response; @@ -42,7 +49,7 @@ class JWTAuthMiddleware implements MiddlewareInterface public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { - if (auth('jwt')->guest()) { + if (auth($this->guard)->guest()) { return $this->response->withStatus(401)->json([ 'code' => 401, 'message' => 'Token authentication does not pass !', diff --git a/app/Middleware/WebSocketAuthMiddleware.php b/app/Middleware/WebSocketAuthMiddleware.php index f119ead..23cad80 100644 --- a/app/Middleware/WebSocketAuthMiddleware.php +++ b/app/Middleware/WebSocketAuthMiddleware.php @@ -30,6 +30,13 @@ class WebSocketAuthMiddleware implements MiddlewareInterface */ protected $container; + /** + * 授权验证守卫 + * + * @var string + */ + private $guard = 'jwt'; + public function __construct(ContainerInterface $container) { $this->container = $container; @@ -38,7 +45,7 @@ class WebSocketAuthMiddleware implements MiddlewareInterface 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'); }