Authenticate.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. /*
  3. * This file is part of the Jiannei/lumen-api-starter.
  4. *
  5. * (c) Jiannei <longjian.huang@foxmail.com>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace App\Http\Middleware;
  11. use App\Repositories\Enums\ResponseCodeEnum;
  12. use Closure;
  13. use Illuminate\Auth\Access\AuthorizationException;
  14. use Illuminate\Contracts\Auth\Factory as Auth;
  15. use Illuminate\Http\Request;
  16. class Authenticate
  17. {
  18. /**
  19. * The authentication guard factory instance.
  20. *
  21. * @var Auth
  22. */
  23. protected $auth;
  24. /**
  25. * Create a new middleware instance.
  26. *
  27. * @param Auth $auth
  28. */
  29. public function __construct(Auth $auth)
  30. {
  31. $this->auth = $auth;
  32. }
  33. /**
  34. * Handle an incoming request.
  35. *
  36. * @param Request $request
  37. * @param Closure $next
  38. * @param string|null $guard
  39. *
  40. * @return mixed
  41. *
  42. * @throws AuthorizationException
  43. */
  44. public function handle($request, Closure $next, $guard = null)
  45. {
  46. if ($this->auth->guard($guard)->guest()) {
  47. abort(ResponseCodeEnum::HTTP_UNAUTHORIZED);
  48. }
  49. return $next($request);
  50. }
  51. }