Authenticate.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 App\Repositories\Models\Log;
  13. use Closure;
  14. use Illuminate\Auth\Access\AuthorizationException;
  15. use Illuminate\Contracts\Auth\Factory as Auth;
  16. use Illuminate\Http\Request;
  17. use Illuminate\Support\Str;
  18. class Authenticate
  19. {
  20. /**
  21. * The authentication guard factory instance.
  22. *
  23. * @var Auth
  24. */
  25. protected $auth;
  26. /**
  27. * Create a new middleware instance.
  28. *
  29. * @param Auth $auth
  30. */
  31. public function __construct(Auth $auth)
  32. {
  33. $this->auth = $auth;
  34. }
  35. /**
  36. * Handle an incoming request.
  37. *
  38. * @param Request $request
  39. * @param Closure $next
  40. * @param string|null $guard
  41. *
  42. * @return mixed
  43. *
  44. * @throws AuthorizationException
  45. */
  46. public function handle($request, Closure $next, $guard = null)
  47. {
  48. if ($this->auth->guard($guard)->guest()) {
  49. abort(ResponseCodeEnum::HTTP_UNAUTHORIZED);
  50. }
  51. return $next($request);
  52. }
  53. }