Skip to content

Commit

Permalink
67. detect robots & fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitrakovich committed Jan 16, 2025
1 parent dc384db commit 31b65fa
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
28 changes: 27 additions & 1 deletion src/app/Http/Middleware/DeviceDetect.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,23 @@
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cookie;
use Jenssegers\Agent\Facades\Agent;

class DeviceDetect
{
/**
* Web ID for robot devices
*/
private const WEB_ID_FOR_ROBOT_DEVICES = '6b8945bed1e18bc4f2d1691ac63f5f56';

/**
* Handle an incoming request.
*
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
$webId = $request->cookie(CookieEnum::DEVICE_ID->value);
$webId = $this->getDeviceWebId($request);
if (!$webId) {
$webId = UserDevice::generateNewWebId($request);
Cookie::queue(
Expand All @@ -32,4 +38,24 @@ public function handle(Request $request, Closure $next)

return $next($request);
}

/**
* Get the web ID for the current device
*/
private function getDeviceWebId(Request $request): ?string
{
if ($this->isRobot($request)) {
return self::WEB_ID_FOR_ROBOT_DEVICES;
}

return $request->cookie(CookieEnum::DEVICE_ID->value);
}

/**
* Check if the current request is from a robot
*/
private function isRobot(Request $request): bool
{
return Agent::isRobot(); // additional checks
}
}
2 changes: 1 addition & 1 deletion src/app/Models/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static function search(string $slug): ?self
*/
public function model(): MorphTo
{
return $this->morphTo();
return $this->morphTo()->withTrashed();
}

/**
Expand Down
8 changes: 7 additions & 1 deletion src/bootstrap/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,15 @@
->encryptCookies(['utm', Cookie::YANDEX_ID->value, Cookie::GOOGLE_ID->value])
->throttleApi(redis: true);

$middleware->web(append: [
$middleware->group('web', [
\Illuminate\Cookie\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\Illuminate\Foundation\Http\Middleware\ValidateCsrfToken::class,
\App\Http\Middleware\DeviceDetect::class,
\App\Http\Middleware\MigrateCartToDevice::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
\Spatie\GoogleTagManager\GoogleTagManagerMiddleware::class,
\App\Http\Middleware\ViewMiddleware::class,
]);
Expand Down
2 changes: 1 addition & 1 deletion src/routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
$url = Url::search($slug);

return $url?->model_type === Product::class
? redirect(status: 301)->route('product.show', $url->model->slug)
? redirect(status: 301)->route('product.show', $url->model()->firstOrFail()->slug)
: app(CatalogController::class)->show($request);
};
Route::get('catalog/city-{city}/{path?}', $check_catalog)->where('path', '[a-zA-Z0-9/_-]+')->name('shop-city');
Expand Down

0 comments on commit 31b65fa

Please sign in to comment.