AdminUserAuthenticate.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. namespace App\Http\Middleware;
  3. use App\User\Facades\SysAdminUserFacade;
  4. use Closure;
  5. use Illuminate\Contracts\Auth\Factory as Auth;
  6. use App\Exceptions\ApiException;
  7. class AdminUserAuthenticate
  8. {
  9. /**
  10. * The authentication guard factory instance.
  11. *
  12. * @var \Illuminate\Contracts\Auth\Factory
  13. */
  14. protected $auth;
  15. /**
  16. * Create a new middleware instance.
  17. *
  18. * @param \Illuminate\Contracts\Auth\Factory $auth
  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. * @throws ApiException
  32. */
  33. public function handle($request, Closure $next, $guard = null)
  34. {
  35. if(empty($request->user())){
  36. throw new ApiException(401);
  37. }
  38. $user = $request->user();
  39. $userId = $user['id'];
  40. $token = $request->header('token');
  41. //如果不是最近登录的token
  42. if (config('app.login_singleton') && !SysAdminUserFacade::isLastToken($userId, $token)) {
  43. SysAdminUserFacade::logout($token);
  44. throw new ApiException(10008);
  45. }
  46. return $next($request);
  47. }
  48. }