From f37d7da8078bf5650eee345b44b9c4e26d841b82 Mon Sep 17 00:00:00 2001 From: Hasbi Ashshidiq Date: Tue, 2 Dec 2025 11:48:01 +0700 Subject: [PATCH 1/3] fix(auth): update `getRecallerId` to cast ID to int also add test for returning integer ID from remember cookie --- src/Illuminate/Auth/Guard.php | 16 +++++++--------- tests/Auth/AuthGuardTest.php | 21 +++++++++++++++++++++ 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/src/Illuminate/Auth/Guard.php b/src/Illuminate/Auth/Guard.php index fd7aca354..76ce1dfbb 100755 --- a/src/Illuminate/Auth/Guard.php +++ b/src/Illuminate/Auth/Guard.php @@ -149,7 +149,6 @@ public function user() // pull the user data on that cookie which serves as a remember cookie on // the application. Once we have a user we can return it to the caller. $recaller = $this->getRecaller(); - if (is_null($user) && ! is_null($recaller)) { $user = $this->getUserByRecaller($recaller); @@ -210,15 +209,14 @@ protected function getRecaller() /** * Get the user ID from the recaller cookie. * - * @return string + * @return int */ - protected function getRecallerId() - { - if ($this->validRecaller($recaller = $this->getRecaller())) - { - return head(explode('|', $recaller)); - } - } + protected function getRecallerId() + { + if ($this->validRecaller($recaller = $this->getRecaller())) { + return (int) head(explode('|', $recaller)); + } + } /** * Determine if the recaller cookie is in a valid format. diff --git a/tests/Auth/AuthGuardTest.php b/tests/Auth/AuthGuardTest.php index 04c63619a..962acf3a8 100755 --- a/tests/Auth/AuthGuardTest.php +++ b/tests/Auth/AuthGuardTest.php @@ -374,4 +374,25 @@ public function testUserUsesRememberCookieIfItExists() $this->assertEquals($user->reveal(), $guard->user()); $this->assertTrue($guard->viaRemember()); } + + public function testGetIdWhenRememberCookieExistsWillReturnIntegerIdFromCookieValue() + { + $request = Request::create('/', 'GET', [], [ + 'remember_82e5d2c56bdd0811318f0cf078b78bfc' => '123|recaller' + ]); + + $this->session->get('login_82e5d2c56bdd0811318f0cf078b78bfc', Argument::any())->will(function ($args) { + return $args[1]; + }); + + $guard = new Guard( + $this->userProvider->reveal(), + $this->session->reveal(), + $request + ); + + $this->assertNotNull($guard->id()); + $this->assertIsInt($guard->id()); + $this->assertSame(123, $guard->id()); + } } From 6600c864ad73e49b8b58858290d100d017f4f05c Mon Sep 17 00:00:00 2001 From: Hasbi Ashshidiq Date: Wed, 3 Dec 2025 11:16:47 +0700 Subject: [PATCH 2/3] refactor: remove default value for getting $id in id() --- src/Illuminate/Auth/Guard.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Auth/Guard.php b/src/Illuminate/Auth/Guard.php index 76ce1dfbb..c3b96d111 100755 --- a/src/Illuminate/Auth/Guard.php +++ b/src/Illuminate/Auth/Guard.php @@ -166,7 +166,7 @@ public function id() { if ($this->loggedOut) return; - $id = $this->session->get($this->getName(), $this->getRecallerId()); + $id = $this->session->get($this->getName()); if (is_null($id) && $this->user()) { From baafa3d1a735313b8f1b9c7830ef9c271c002364 Mon Sep 17 00:00:00 2001 From: Hasbi Ashshidiq Date: Wed, 3 Dec 2025 11:21:33 +0700 Subject: [PATCH 3/3] test: adjust remember cookie test to add user stub and remove default value from session --- tests/Auth/AuthGuardTest.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/tests/Auth/AuthGuardTest.php b/tests/Auth/AuthGuardTest.php index 962acf3a8..454e95fb2 100755 --- a/tests/Auth/AuthGuardTest.php +++ b/tests/Auth/AuthGuardTest.php @@ -380,10 +380,12 @@ public function testGetIdWhenRememberCookieExistsWillReturnIntegerIdFromCookieVa $request = Request::create('/', 'GET', [], [ 'remember_82e5d2c56bdd0811318f0cf078b78bfc' => '123|recaller' ]); + $user = $this->prophesize(UserInterface::class); + $user->getAuthIdentifier()->willReturn(123); - $this->session->get('login_82e5d2c56bdd0811318f0cf078b78bfc', Argument::any())->will(function ($args) { - return $args[1]; - }); + $this->userProvider + ->retrieveByToken(123, 'recaller') + ->willReturn($user->reveal()); $guard = new Guard( $this->userProvider->reveal(),