AuthFilter.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php namespace App\Http\Filters;
  2. use Illuminate\Http\Request;
  3. use Illuminate\Routing\Route;
  4. use Illuminate\Contracts\Auth\Authenticator;
  5. use Illuminate\Contracts\Routing\ResponseFactory;
  6. class AuthFilter {
  7. /**
  8. * The authenticator implementation.
  9. *
  10. * @var Authenticator
  11. */
  12. protected $auth;
  13. /**
  14. * The response factory implementation.
  15. *
  16. * @var ResponseFactory
  17. */
  18. protected $response;
  19. /**
  20. * Create a new filter instance.
  21. *
  22. * @param Authenticator $auth
  23. * @param ResponseFactory $response
  24. * @return void
  25. */
  26. public function __construct(Authenticator $auth,
  27. ResponseFactory $response)
  28. {
  29. $this->auth = $auth;
  30. $this->response = $response;
  31. }
  32. /**
  33. * Run the request filter.
  34. *
  35. * @param \Illuminate\Routing\Route $route
  36. * @param \Illuminate\Http\Request $request
  37. * @return mixed
  38. */
  39. public function filter(Route $route, Request $request)
  40. {
  41. if ($this->auth->guest())
  42. {
  43. if ($request->ajax())
  44. {
  45. return $this->response->make('Unauthorized', 401);
  46. }
  47. else
  48. {
  49. return $this->response->redirectGuest('auth/login');
  50. }
  51. }
  52. }
  53. }