Authenticate.php 1022 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. namespace App\Http\Middleware;
  3. use Closure;
  4. use Illuminate\Contracts\Auth\Factory as Auth;
  5. class Authenticate
  6. {
  7. /**
  8. * The authentication guard factory instance.
  9. *
  10. * @var \Illuminate\Contracts\Auth\Factory
  11. */
  12. protected $auth;
  13. /**
  14. * Create a new middleware instance.
  15. *
  16. * @param \Illuminate\Contracts\Auth\Factory $auth
  17. * @return void
  18. */
  19. public function __construct(Auth $auth)
  20. {
  21. $this->auth = $auth;
  22. }
  23. /**
  24. * Handle an incoming request.
  25. *
  26. * @param \Illuminate\Http\Request $request
  27. * @param \Closure $next
  28. * @param string|null $guard
  29. * @return mixed
  30. */
  31. public function handle($request, Closure $next, $guard = null)
  32. {
  33. if ($this->auth->guard($guard)->guest()) {
  34. $error = [
  35. 'message' => '请重新登录',
  36. 'code' => 401,
  37. ];
  38. return response()->json($error);
  39. }
  40. return $next($request);
  41. }
  42. }