AdminAuthMiddleware.php 966 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. <?php
  2. namespace App\Http\Middleware;
  3. use App\Models\User\User;
  4. use App\Modules\Admin\Services\AuthService;
  5. use Closure;
  6. use Illuminate\Auth\AuthenticationException;
  7. use Illuminate\Http\Request;
  8. use Illuminate\Support\Facades\Auth;
  9. class AdminAuthMiddleware
  10. {
  11. public function handle(Request $request, Closure $next)
  12. {
  13. $token = $request->header('Authorization');
  14. if (empty($token)) {
  15. throw new AuthenticationException("need authorization");
  16. }
  17. $uid = app(AuthService::class)->decryptToken(substr($token, 7));
  18. $u = User::find($uid);
  19. if (is_null($u)) {
  20. return response()->json([
  21. "code" => 401,
  22. ]);
  23. }
  24. Auth::login($u);
  25. logger()->info("admin operation log", [
  26. "url" => $request->url(),
  27. "method" => $request->method(),
  28. "request" => $request->all(),
  29. ]);
  30. return $next($request);
  31. }
  32. }