Skip to content

Commit c2b0aa7

Browse files
authored
Merge pull request #259 from PHP-Open-Source-Saver/bugfix/258-logout-with-expired-token
2 parents b163d51 + f8c36bf commit c2b0aa7

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

CHANGELOG.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,27 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77
You can find and compare releases at the GitHub release page.
88

99
## [Unreleased]
10-
- SetSecret regenerates config with new secret in the Lcobucci provider
10+
11+
### Added
12+
- Fixes #259 - Can't logout with an expired token
13+
14+
### Removed
15+
16+
## [2.6.0] 2024-07-11
17+
18+
### Added
19+
- New `getUserId` method
20+
21+
## [2.5.0] 2024-07-03
22+
23+
### Added
1124
- Refresh iat claim when refreshing a token
1225

26+
## [2.4.0] 2024-05-27
27+
1328
### Added
1429
- Support for lcobucci/jwt^5.0 (and dropped support for ^4.0)
15-
- New `getUserId` method
30+
- SetSecret regenerates config with new secret in the Lcobucci provider
1631

1732
## [2.3.0] 2024-05-09
1833

src/JWTGuard.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,11 @@ public function login(JWTSubject $user)
200200
*/
201201
public function logout($forceForever = false)
202202
{
203-
$this->requireToken()->invalidate($forceForever);
203+
try {
204+
$this->requireToken()->invalidate($forceForever);
205+
} catch (JWTException $e) {
206+
// Proceed with the logout as normal if we can't invalidate the token
207+
}
204208

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

tests/JWTGuardTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Illuminate\Http\Request;
2323
use Mockery\LegacyMockInterface;
2424
use PHPOpenSourceSaver\JWTAuth\Exceptions\JWTException;
25+
use PHPOpenSourceSaver\JWTAuth\Exceptions\TokenExpiredException;
2526
use PHPOpenSourceSaver\JWTAuth\Exceptions\UserNotDefinedException;
2627
use PHPOpenSourceSaver\JWTAuth\Factory;
2728
use PHPOpenSourceSaver\JWTAuth\JWT;
@@ -353,6 +354,25 @@ public function testItShouldLogoutTheUserByInvalidatingTheToken()
353354
$this->assertNull($this->guard->getUser());
354355
}
355356

357+
public function testItShouldLogoutTheUserEvenWithExpiredToken()
358+
{
359+
$this->jwt->shouldReceive('setRequest')->andReturn($this->jwt);
360+
$this->jwt->shouldReceive('getToken')->once()->andReturn(true);
361+
$this->jwt->shouldReceive('invalidate')->andThrow(TokenExpiredException::class);
362+
$this->jwt->shouldReceive('unsetToken')->once();
363+
364+
$this->eventDispatcher->shouldReceive('dispatch')
365+
->never()
366+
->with(\Mockery::type(Authenticated::class));
367+
368+
$this->eventDispatcher->shouldReceive('dispatch')
369+
->once()
370+
->with(\Mockery::type(Logout::class));
371+
372+
$this->guard->logout();
373+
$this->assertNull($this->guard->getUser());
374+
}
375+
356376
public function testItShouldRefreshTheToken()
357377
{
358378
$this->jwt->shouldReceive('setRequest')->andReturn($this->jwt);

0 commit comments

Comments
 (0)