1234567891011121314151617181920212223242526272829303132333435363738 |
- <?php
- namespace App\Http\Middleware;
- use App\Models\User\User;
- use App\Modules\Admin\Services\AuthService;
- use Closure;
- use Illuminate\Auth\AuthenticationException;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Auth;
- class AdminAuthMiddleware
- {
- public function handle(Request $request, Closure $next)
- {
- $token = $request->header('Authorization');
- if (empty($token)) {
- throw new AuthenticationException("need authorization");
- }
- $uid = app(AuthService::class)->decryptToken(substr($token, 7));
- $u = User::find($uid);
- if (is_null($u)) {
- return response()->json([
- "code" => 401,
- ]);
- }
- Auth::login($u);
- logger()->info("admin operation log", [
- "url" => $request->url(),
- "method" => $request->method(),
- "request" => $request->all(),
- ]);
- return $next($request);
- }
- }
|