Skip to content

Commit

Permalink
73. add ip check for new devices
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitrakovich committed Jan 30, 2025
1 parent 9e8716d commit 286bbfe
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/app/Http/Middleware/DeviceDetect.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Models\User\Device as UserDevice;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Cookie;
use Jenssegers\Agent\Facades\Agent;

Expand All @@ -30,6 +31,7 @@ public function handle(Request $request, Closure $next)
Cookie::queue(
cookie(CookieEnum::DEVICE_ID->value, $webId, UserDevice::COOKIE_LIFE_TIME, '/')
);
Cache::put($this->getNewDeviceCacheKey($request), $webId, now()->addHours(6));
}

DeviceFacade::setDevice(
Expand Down Expand Up @@ -58,6 +60,23 @@ private function getDeviceWebId(Request $request): ?string
*/
private function isRobot(Request $request): bool
{
return Agent::isRobot(); // additional checks
return Agent::isRobot() || $this->isDeviceRecognizedByIpCache($request);
}

/**
* Get the cache key for the new device
*/
private function getNewDeviceCacheKey(Request $request): string
{
return "newDeviceIp:{$request->ip()}";
}

/**
* Check if the device is recognized by IP cache
*/
private function isDeviceRecognizedByIpCache(Request $request): bool
{
return !$request->cookie(CookieEnum::DEVICE_ID->value)
&& Cache::has($this->getNewDeviceCacheKey($request));
}
}

0 comments on commit 286bbfe

Please sign in to comment.