Skip to content

Commit

Permalink
Update session check on middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
mckenziearts committed May 25, 2024
1 parent 668da2a commit d28b97d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Tests
name: tests

on: push

Expand Down
10 changes: 5 additions & 5 deletions src/DataObject/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
final class Connection
{
public function __construct(
public string $connectionType,
public int $autonomousSystemNumber,
public string $autonomousSystemOrganization,
public string $ispName,
public string $organizationName,
public ?string $connectionType,
public ?int $autonomousSystemNumber,
public ?string $autonomousSystemOrganization,
public ?string $ispName,
public ?string $organizationName,
) {
}
}
19 changes: 18 additions & 1 deletion src/Middleware/AbstractIpGeolocation.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,25 @@ public function handle($request, Closure $next): mixed
(new IpGeolocation($request))->initialize()
);

session()->put('abstract-ip-geolocation', $response);
$this->manageSession($response);

return $next($request);
}

public function manageSession(GeolocationData $geolocation): void
{
$sessionKey = 'abstract-ip-geolocation';

if (session()->exists($sessionKey)) {
/** @var GeolocationData $currentValue */
$currentValue = session()->get($sessionKey);

if ($currentValue->ipAddress !== $geolocation->ipAddress) {
session()->forget($sessionKey);
session()->put($sessionKey, $geolocation);
}
} else {
session()->put($sessionKey, $geolocation);
}
}
}

0 comments on commit d28b97d

Please sign in to comment.