From 24f08057f86981a8a7ba675db7d7df643bc9acd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=80=99=E5=AD=A6=E6=9D=B0?= Date: Wed, 2 May 2018 16:37:30 +0800 Subject: [PATCH] add xml request --- src/PdAuth/Middleware/Authenticate.php | 21 ++++++++++++++------- src/PdAuth/PdAuthServiceProvider.php | 3 ++- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/PdAuth/Middleware/Authenticate.php b/src/PdAuth/Middleware/Authenticate.php index 6acd445..b0225ed 100644 --- a/src/PdAuth/Middleware/Authenticate.php +++ b/src/PdAuth/Middleware/Authenticate.php @@ -37,7 +37,7 @@ class Authenticate * @param string|null $guard * @return mixed */ - public function handle($request, Closure $next, $guard = 'auth') + public function handle($request, Closure $next, $guard = null) { //oauth 回调 $code = $request->input('pd_code'); @@ -55,22 +55,29 @@ class Authenticate } $qs .= $v . '&'; } - abort(302, '', [ - 'Location' => $request->getSchemeAndHttpHost() . $request->getBaseUrl() . $request->getPathInfo() . $qs, - ]); + + if (!$request->isXmlHttpRequest()) { + abort(302, '', [ + 'Location' => $request->getSchemeAndHttpHost() . $request->getBaseUrl() . $request->getPathInfo() . $qs, + ]); + } } } //登录状态检测 if ($this->auth->guard($guard)->guest()) { + $redirect = $request->input('redirect', $request->getUri()); if ($request->isXmlHttpRequest()) { return response()->json([ 'code' => 401, 'msg' => 'need login', - 'data' => null, + 'data' => [ + 'url' => app('pd.auth')->connect($redirect), + ], ]); + } else { + return redirect(app('pd.auth')->connect($redirect)); } - return redirect(app('pd.auth')->connect($request->getUri())); } //权限检测 @@ -103,7 +110,7 @@ class Authenticate // ]); // } // api_abort(403, '无权访问,请联系管理员授权'); - + return $next($request); } } diff --git a/src/PdAuth/PdAuthServiceProvider.php b/src/PdAuth/PdAuthServiceProvider.php index 26542a1..c45dc7e 100644 --- a/src/PdAuth/PdAuthServiceProvider.php +++ b/src/PdAuth/PdAuthServiceProvider.php @@ -38,12 +38,13 @@ class PdAuthServiceProvider extends ServiceProvider } return null; }); + // $config = $this->app['config']['auth']; if (!isset($config['guards']['auth'])) { config(['auth.guards.auth' => ['driver' => 'auth']]); - config(['auth.defaults.guard' => 'auth']); + $this->app['auth']->shouldUse('auth'); } }