Skip to content

Commit

Permalink
Merge pull request #259 from PHP-Open-Source-Saver/bugfix/258-logout-…
Browse files Browse the repository at this point in the history
…with-expired-token
  • Loading branch information
Messhias authored Jul 25, 2024
2 parents b163d51 + f8c36bf commit c2b0aa7
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
19 changes: 17 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,27 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
You can find and compare releases at the GitHub release page.

## [Unreleased]
- SetSecret regenerates config with new secret in the Lcobucci provider

### Added
- Fixes #259 - Can't logout with an expired token

### Removed

## [2.6.0] 2024-07-11

### Added
- New `getUserId` method

## [2.5.0] 2024-07-03

### Added
- Refresh iat claim when refreshing a token

## [2.4.0] 2024-05-27

### Added
- Support for lcobucci/jwt^5.0 (and dropped support for ^4.0)
- New `getUserId` method
- SetSecret regenerates config with new secret in the Lcobucci provider

## [2.3.0] 2024-05-09

Expand Down
6 changes: 5 additions & 1 deletion src/JWTGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,11 @@ public function login(JWTSubject $user)
*/
public function logout($forceForever = false)
{
$this->requireToken()->invalidate($forceForever);
try {
$this->requireToken()->invalidate($forceForever);
} catch (JWTException $e) {
// Proceed with the logout as normal if we can't invalidate the token
}

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

Expand Down
20 changes: 20 additions & 0 deletions tests/JWTGuardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Illuminate\Http\Request;
use Mockery\LegacyMockInterface;
use PHPOpenSourceSaver\JWTAuth\Exceptions\JWTException;
use PHPOpenSourceSaver\JWTAuth\Exceptions\TokenExpiredException;
use PHPOpenSourceSaver\JWTAuth\Exceptions\UserNotDefinedException;
use PHPOpenSourceSaver\JWTAuth\Factory;
use PHPOpenSourceSaver\JWTAuth\JWT;
Expand Down Expand Up @@ -353,6 +354,25 @@ public function testItShouldLogoutTheUserByInvalidatingTheToken()
$this->assertNull($this->guard->getUser());
}

public function testItShouldLogoutTheUserEvenWithExpiredToken()
{
$this->jwt->shouldReceive('setRequest')->andReturn($this->jwt);
$this->jwt->shouldReceive('getToken')->once()->andReturn(true);
$this->jwt->shouldReceive('invalidate')->andThrow(TokenExpiredException::class);
$this->jwt->shouldReceive('unsetToken')->once();

$this->eventDispatcher->shouldReceive('dispatch')
->never()
->with(\Mockery::type(Authenticated::class));

$this->eventDispatcher->shouldReceive('dispatch')
->once()
->with(\Mockery::type(Logout::class));

$this->guard->logout();
$this->assertNull($this->guard->getUser());
}

public function testItShouldRefreshTheToken()
{
$this->jwt->shouldReceive('setRequest')->andReturn($this->jwt);
Expand Down

0 comments on commit c2b0aa7

Please sign in to comment.