Skip to content

Commit

Permalink
[5.8] Send LogoutOtherDevices event when request is made.
Browse files Browse the repository at this point in the history
This would allow developers to manages other authentications to react to this request
such as `Passport`, where the application may choose to revoke all users access_token etc.

Signed-off-by: Mior Muhammad Zaki <[email protected]>
  • Loading branch information
crynobone committed Mar 12, 2019
1 parent b424f49 commit 7e6c443
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/Illuminate/Auth/Events/LogoutOtherDevices.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Illuminate\Auth\Events;

use Illuminate\Queue\SerializesModels;

class LogoutOtherDevices
{
use SerializesModels;

/**
* The authentication guard name.
*
* @var string
*/
public $guard;

/**
* The authenticated user.
*
* @var \Illuminate\Contracts\Auth\Authenticatable
*/
public $user;

/**
* Create a new event instance.
*
* @param string $guard
* @param \Illuminate\Contracts\Auth\Authenticatable $user
* @return void
*/
public function __construct($guard, $user)
{
$this->user = $user;
$this->guard = $guard;
}
}
17 changes: 17 additions & 0 deletions src/Illuminate/Auth/SessionGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,8 @@ public function logoutOtherDevices($password, $attribute = 'password')

$this->queueRecallerCookie($this->user());

$this->fireLogoutOtherDevicesEvent($this->user());

return $result;
}

Expand Down Expand Up @@ -615,6 +617,21 @@ protected function fireAuthenticatedEvent($user)
}
}

/**
* Fire the logout other devices event if the dispatcher is set.
*
* @param \Illuminate\Contracts\Auth\Authenticatable $user
* @return void
*/
protected function fireLogoutOtherDevicesEvent($user)
{
if (isset($this->events)) {
$this->events->dispatch(new Events\LogoutOtherDevices(
$this->name, $user
));
}
}

/**
* Fire the failed authentication attempt event with the given arguments.
*
Expand Down
22 changes: 22 additions & 0 deletions tests/Integration/Auth/AuthenticationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Illuminate\Auth\EloquentUserProvider;
use Illuminate\Auth\Events\Authenticated;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Auth\Events\LogoutOtherDevices;
use Illuminate\Tests\Integration\Auth\Fixtures\AuthenticationTestUser;

/**
Expand Down Expand Up @@ -185,6 +186,27 @@ public function test_logging_out()
});
}

public function test_logging_out_other_devices()
{
Event::fake();

$this->app['auth']->loginUsingId(1);

$user = $this->app['auth']->user();

$this->assertEquals(1, $user->id);

$this->app['auth']->logoutOtherDevices('adifferentpassword');
$this->assertEquals(1, $user->id);

Event::assertDispatched(LogoutOtherDevices::class, function ($event) {
$this->assertEquals('web', $event->guard);
$this->assertEquals(1, $event->user->id);

return true;
});
}

public function test_logging_in_out_via_attempt_remembering()
{
$this->assertTrue(
Expand Down

0 comments on commit 7e6c443

Please sign in to comment.