Authenticated.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php namespace App\Http\Middleware;
  2. use Closure;
  3. use Illuminate\Contracts\Auth\Guard;
  4. use Illuminate\Contracts\Routing\Middleware;
  5. use Illuminate\Contracts\Routing\ResponseFactory;
  6. class Authenticated implements Middleware {
  7. /**
  8. * The Guard implementation.
  9. *
  10. * @var Guard
  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 Guard $auth
  23. * @param ResponseFactory $response
  24. * @return void
  25. */
  26. public function __construct(Guard $auth,
  27. ResponseFactory $response)
  28. {
  29. $this->auth = $auth;
  30. $this->response = $response;
  31. }
  32. /**
  33. * Handle an incoming request.
  34. *
  35. * @param \Illuminate\Http\Request $request
  36. * @param \Closure $next
  37. * @return mixed
  38. */
  39. public function handle($request, Closure $next)
  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. return $next($request);
  53. }
  54. }