Skip to content

Update session check on middleware #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
}
Loading