Authenticate.php 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. namespace Illuminate\Auth\Middleware;
  3. use Closure;
  4. use Illuminate\Auth\AuthenticationException;
  5. use Illuminate\Contracts\Auth\Factory as Auth;
  6. class Authenticate
  7. {
  8. /**
  9. * The authentication factory instance.
  10. *
  11. * @var \Illuminate\Contracts\Auth\Factory
  12. */
  13. protected $auth;
  14. /**
  15. * Create a new middleware instance.
  16. *
  17. * @param \Illuminate\Contracts\Auth\Factory $auth
  18. * @return void
  19. */
  20. public function __construct(Auth $auth)
  21. {
  22. $this->auth = $auth;
  23. }
  24. /**
  25. * Handle an incoming request.
  26. *
  27. * @param \Illuminate\Http\Request $request
  28. * @param \Closure $next
  29. * @param string[] ...$guards
  30. * @return mixed
  31. *
  32. * @throws \Illuminate\Auth\AuthenticationException
  33. */
  34. public function handle($request, Closure $next, ...$guards)
  35. {
  36. $this->authenticate($guards);
  37. return $next($request);
  38. }
  39. /**
  40. * Determine if the user is logged in to any of the given guards.
  41. *
  42. * @param array $guards
  43. * @return void
  44. *
  45. * @throws \Illuminate\Auth\AuthenticationException
  46. */
  47. protected function authenticate(array $guards)
  48. {
  49. if (empty($guards)) {
  50. return $this->auth->authenticate();
  51. }
  52. foreach ($guards as $guard) {
  53. if ($this->auth->guard($guard)->check()) {
  54. return $this->auth->shouldUse($guard);
  55. }
  56. }
  57. throw new AuthenticationException('Unauthenticated.', $guards);
  58. }
  59. }