Skip to content

Commit 2286fc8

Browse files
authored
Merge pull request #15 from dicoding-dev/bugfix/cast-id-from-remember-token-to-int
fix(auth): update `getRecallerId` to cast ID to int
2 parents fda6610 + baafa3d commit 2286fc8

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

src/Illuminate/Auth/Guard.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,6 @@ public function user()
149149
// pull the user data on that cookie which serves as a remember cookie on
150150
// the application. Once we have a user we can return it to the caller.
151151
$recaller = $this->getRecaller();
152-
153152
if (is_null($user) && ! is_null($recaller))
154153
{
155154
$user = $this->getUserByRecaller($recaller);
@@ -167,7 +166,7 @@ public function id()
167166
{
168167
if ($this->loggedOut) return;
169168

170-
$id = $this->session->get($this->getName(), $this->getRecallerId());
169+
$id = $this->session->get($this->getName());
171170

172171
if (is_null($id) && $this->user())
173172
{
@@ -210,15 +209,14 @@ protected function getRecaller()
210209
/**
211210
* Get the user ID from the recaller cookie.
212211
*
213-
* @return string
212+
* @return int
214213
*/
215-
protected function getRecallerId()
216-
{
217-
if ($this->validRecaller($recaller = $this->getRecaller()))
218-
{
219-
return head(explode('|', $recaller));
220-
}
221-
}
214+
protected function getRecallerId()
215+
{
216+
if ($this->validRecaller($recaller = $this->getRecaller())) {
217+
return (int) head(explode('|', $recaller));
218+
}
219+
}
222220

223221
/**
224222
* Determine if the recaller cookie is in a valid format.

tests/Auth/AuthGuardTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,4 +374,27 @@ public function testUserUsesRememberCookieIfItExists()
374374
$this->assertEquals($user->reveal(), $guard->user());
375375
$this->assertTrue($guard->viaRemember());
376376
}
377+
378+
public function testGetIdWhenRememberCookieExistsWillReturnIntegerIdFromCookieValue()
379+
{
380+
$request = Request::create('/', 'GET', [], [
381+
'remember_82e5d2c56bdd0811318f0cf078b78bfc' => '123|recaller'
382+
]);
383+
$user = $this->prophesize(UserInterface::class);
384+
$user->getAuthIdentifier()->willReturn(123);
385+
386+
$this->userProvider
387+
->retrieveByToken(123, 'recaller')
388+
->willReturn($user->reveal());
389+
390+
$guard = new Guard(
391+
$this->userProvider->reveal(),
392+
$this->session->reveal(),
393+
$request
394+
);
395+
396+
$this->assertNotNull($guard->id());
397+
$this->assertIsInt($guard->id());
398+
$this->assertSame(123, $guard->id());
399+
}
377400
}

0 commit comments

Comments
 (0)