Skip to content

Commit 734f269

Browse files
authored
Merge pull request #298 from NielsJanssen/main
feat: Trigger Authenticated event when loading user from token
2 parents 648306c + 0b09cd8 commit 734f269

3 files changed

Lines changed: 43 additions & 12 deletions

File tree

CHANGELOG.md

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,33 @@ 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](https://github.com/PHP-Open-Source-Saver/jwt-auth/releases).
88

99
## [Unreleased]
10+
11+
### Added
12+
- Trigger Authenticated event when loading user from token
13+
14+
## [2.9.0] 2026-03-06
15+
Please see (https://github.com/PHP-Open-Source-Saver/jwt-auth/releases/tag/2.9.0)
16+
17+
### Added
18+
- Updating composer and CI for Laravel 13
19+
20+
## [2.8.3] 2025-10-15
21+
Please see (https://github.com/PHP-Open-Source-Saver/jwt-auth/releases/tag/2.8.3)
22+
23+
### Fixed
24+
- Implement config variable to allow iat to remain unchanged claim when refreshing a token
25+
26+
## [2.8.2] 2025-03-19
27+
Please see (https://github.com/PHP-Open-Source-Saver/jwt-auth/releases/tag/2.8.2)
28+
1029
### Fixed
11-
- Fixed the return type of getMinutesUntilExpired in BlackList, which returned a float instead of an int when using Carbon v2.
12-
- Fixed PHPStan issue in JWTGenerateSecretCommand by ensuring displayKey($key); is called before returning, avoiding returning a void method.
13-
- Fixed missing return true; statements in validatePayload() and validateRefresh() methods of Expiration.php, IssuedAt.php, and NotBefore.php to resolve PHPStan errors.
14-
- Fixed PHPStan error related to new static() by refactoring hasAllClaims method in Collection class.
30+
- Fix PHPStan Issues
31+
32+
## [2.8.1] 2025-02-28
33+
Please see (https://github.com/PHP-Open-Source-Saver/jwt-auth/releases/tag/2.8.1)
1534

35+
### Fixed
36+
- Fixed the return type of getMinutesUntilExpired in BlackList, which returned a float instead of an int when using Carbon v2.
1637

1738
## [2.8.0] 2025-02-11
1839
Please see (https://github.com/PHP-Open-Source-Saver/jwt-auth/releases/tag/2.8.0)

src/JWTGuard.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,9 @@ public function user()
104104
&& ($payload = $this->jwt->check(true))
105105
&& $this->validateSubject()
106106
) {
107-
return $this->user = $this->provider->retrieveById($payload['sub']);
107+
$this->setUser($this->provider->retrieveById($payload['sub']));
108+
109+
return $this->user;
108110
}
109111
}
110112

tests/JWTGuardTest.php

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,20 @@ public function testItShouldGetTheAuthenticatedUserIfAValidTokenIsProvided()
7979
$this->provider->shouldReceive('retrieveById')
8080
->once()
8181
->with(1)
82-
->andReturn((object) ['id' => 1]);
82+
->andReturn(new LaravelUserStub());
8383

84-
$this->assertSame(1, $this->guard->user()->id);
84+
$this->eventDispatcher->shouldReceive('dispatch')
85+
->once()
86+
->with(\Mockery::type(Authenticated::class));
87+
88+
$this->assertSame(1, $this->guard->user()->getAuthIdentifier());
8589

8690
// check that the user is stored on the object next time round
87-
$this->assertSame(1, $this->guard->user()->id);
91+
$this->assertSame(1, $this->guard->user()->getAuthIdentifier());
8892
$this->assertTrue($this->guard->check());
8993

9094
// also make sure userOrFail does not fail
91-
$this->assertSame(1, $this->guard->userOrFail()->id);
95+
$this->assertSame(1, $this->guard->userOrFail()->getAuthIdentifier());
9296
}
9397

9498
public function testItShouldGetTheAuthenticatedUserIfAValidTokenIsProvidedAndNotThrowAnException()
@@ -110,12 +114,16 @@ public function testItShouldGetTheAuthenticatedUserIfAValidTokenIsProvidedAndNot
110114
$this->provider->shouldReceive('retrieveById')
111115
->once()
112116
->with(1)
113-
->andReturn((object) ['id' => 1]);
117+
->andReturn(new LaravelUserStub());
118+
119+
$this->eventDispatcher->shouldReceive('dispatch')
120+
->once()
121+
->with(\Mockery::type(Authenticated::class));
114122

115-
$this->assertSame(1, $this->guard->userOrFail()->id);
123+
$this->assertSame(1, $this->guard->userOrFail()->getAuthIdentifier());
116124

117125
// check that the user is stored on the object next time round
118-
$this->assertSame(1, $this->guard->userOrFail()->id);
126+
$this->assertSame(1, $this->guard->userOrFail()->getAuthIdentifier());
119127
$this->assertTrue($this->guard->check());
120128
}
121129

0 commit comments

Comments
 (0)