Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add getUserId method #257

Merged
merged 1 commit into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ You can find and compare releases at the GitHub release page.

### Added
- Support for lcobucci/jwt^5.0 (and dropped support for ^4.0)
- New `getUserId` method

## [2.3.0] 2024-05-09

Expand Down
20 changes: 20 additions & 0 deletions src/JWTGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,26 @@ public function user()
}
}

/**
* @return int|string|null
*/
public function getUserId()
{
if (null !== $this->user) {
return $this->user->getAuthIdentifier();
}

if (
$this->jwt->setRequest($this->request)->getToken()
&& ($payload = $this->jwt->check(true))
&& $this->validateSubject()
) {
return $payload['sub'];
}

return null;
}

/**
* Get the currently authenticated user or throws an exception.
*
Expand Down
31 changes: 31 additions & 0 deletions tests/JWTGuardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,37 @@ public function testItShouldThrowAnExceptionIfNoTokenIsProvided()
$this->guard->userOrFail(); // throws the exception
}

public function testItShouldGetTheAuthenticatedUserIdIfAValidTokenIsProvided()
{
$payload = \Mockery::mock(Payload::class);
$payload->shouldReceive('offsetGet')->once()->with('sub')->andReturn(1);

$this->jwt->shouldReceive('setRequest')->andReturn($this->jwt);
$this->jwt->shouldReceive('getToken')->once()->andReturn('foo.bar.baz');
$this->jwt->shouldReceive('check')->once()->with(true)->andReturn($payload);
$this->jwt->shouldReceive('checkSubjectModel')
->once()
->with('\PHPOpenSourceSaver\JWTAuth\Test\Stubs\LaravelUserStub')
->andReturn(true);

$this->provider->shouldReceive('getModel')
->once()
->andReturn('\PHPOpenSourceSaver\JWTAuth\Test\Stubs\LaravelUserStub');

$this->assertSame(1, $this->guard->getUserId());
}

public function testItShouldReturnNullForUserIdIfNoTokenIsProvided()
{
$this->jwt->shouldReceive('setRequest')->andReturn($this->jwt);
$this->jwt->shouldReceive('getToken')->andReturn(false);
$this->jwt->shouldReceive('check')->never();
$this->jwt->shouldReceive('getPayload->get')->never();
$this->provider->shouldReceive('retrieveById')->never();

$this->assertNull($this->guard->getUserId());
}

public function testItShouldReturnATokenIfCredentialsAreOkAndUserIsFound()
{
$credentials = ['foo' => 'bar', 'baz' => 'bob'];
Expand Down