AuthLoginMiddleware.php 660 B

12345678910111213141516171819202122232425262728293031
  1. <?php
  2. namespace App\Http\Middleware;
  3. use App\Models\User\User;
  4. use App\Modules\Admin\Services\AuthService;
  5. use Closure;
  6. use Illuminate\Auth\AuthenticationException;
  7. use Illuminate\Http\Request;
  8. use Illuminate\Support\Facades\Auth;
  9. class AuthLoginMiddleware
  10. {
  11. public function handle(Request $request, Closure $next)
  12. {
  13. $token = $request->header('Authorization');
  14. if (empty($token)) {
  15. return $next($request);
  16. }
  17. $uid = app(AuthService::class)->decryptToken(substr($token, 7));
  18. $u = User::find($uid);
  19. if ($u) {
  20. Auth::login($u);
  21. }
  22. return $next($request);
  23. }
  24. }