Authenticate.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. namespace App\Http\Middleware;
  3. use Closure;
  4. use Illuminate\Contracts\Auth\Factory as Auth;
  5. use Illuminate\Support\Facades\Log;
  6. class Authenticate
  7. {
  8. /**
  9. * The authentication guard 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|null $guard
  30. * @return mixed
  31. */
  32. public function handle($request, Closure $next, $guard = null)
  33. {
  34. if ($this->auth->guard($guard)->guest()) {
  35. $error = [
  36. 'message' => '请重新登录',
  37. 'code' => 401,
  38. ];
  39. return response()->json($error)->setStatusCode(401);
  40. }
  41. $user = $this->auth->user();
  42. return $next($request);
  43. }
  44. }