Skip to content

Commit

Permalink
feat: clear session cookie on logout
Browse files Browse the repository at this point in the history
  • Loading branch information
addeeandra committed Oct 10, 2024
1 parent 4dd2fe8 commit cd59e26
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Cookie/AbstractCookieLayer.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ public function setCookie(string $key): void
cookie()->queue(cookie($this->getName(), $this->generate($key), 60 * 24 * 365));
}

public function clearCookie(): void
{
cookie()->queue(cookie()->forget($this->getName()));
}

public function verify(string $key): bool
{
if ($this->getCookieValue() === '' || $this->getCookieValue() === '0') {
Expand Down
17 changes: 17 additions & 0 deletions src/Listeners/ClearStoredSessionIP.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Dentro\Paranoia\Listeners;

use Dentro\Paranoia\Cookie\CookieLayer;
use Dentro\Paranoia\Paranoia;
use Illuminate\Auth\Events\Logout;

class ClearStoredSessionIP
{
public function handle(Logout $event, Paranoia $driver): void
{
if ($driver->useCookieForIP()) {
$driver->cookieLayer(CookieLayer::IP)->clearCookie();
}
}
}
17 changes: 17 additions & 0 deletions src/Listeners/ClearStoredSessionUserAgent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Dentro\Paranoia\Listeners;

use Dentro\Paranoia\Cookie\CookieLayer;
use Dentro\Paranoia\Paranoia;
use Illuminate\Auth\Events\Logout;

class ClearStoredSessionUserAgent
{
public function handle(Logout $event, Paranoia $driver): void
{
if ($driver->useCookieForUserAgent()) {
$driver->cookieLayer(CookieLayer::USER_AGENT)->clearCookie();
}
}
}
7 changes: 7 additions & 0 deletions src/Providers/EventServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@

namespace Dentro\Paranoia\Providers;

use Dentro\Paranoia\Listeners\ClearStoredSessionIP;
use Dentro\Paranoia\Listeners\ClearStoredSessionUserAgent;
use Dentro\Paranoia\Listeners\StoreSessionIP;
use Dentro\Paranoia\Listeners\StoreSessionUserAgent;
use Illuminate\Auth\Events\Login;
use Illuminate\Auth\Events\Logout;
use Illuminate\Events\EventServiceProvider as ServiceProvider;

class EventServiceProvider extends ServiceProvider
Expand All @@ -19,5 +22,9 @@ class EventServiceProvider extends ServiceProvider
StoreSessionIP::class,
StoreSessionUserAgent::class,
],
Logout::class => [
ClearStoredSessionIP::class,
ClearStoredSessionUserAgent::class,
],
];
}

0 comments on commit cd59e26

Please sign in to comment.