优化代码
parent
60ea1688b7
commit
b65d427011
|
@ -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()
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
|
|
@ -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');
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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 !',
|
||||
|
|
|
@ -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');
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue