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

[10.x] Remove session on authenticatable deletion v2 #47141

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Illuminate/Auth/SessionGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ public function user()
}
}

if (is_null($this->user)) {
$this->clearUserDataFromStorage();
}

return $this->user;
}

Expand Down
6 changes: 6 additions & 0 deletions tests/Auth/AuthGuardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,9 @@ public function testAuthenticateThrowsWhenUserIsNull()
$this->expectExceptionMessage('Unauthenticated.');

$guard = $this->getGuard();
$guard->setCookieJar($cookies = m::mock(CookieJar::class));
$cookies->shouldReceive('unqueue')->once();
Comment on lines +265 to +266
Copy link
Contributor

@mfn mfn Jun 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm seeing a problem here in my application since this change: if I hit a route which requires auth but the request is not auth'd (just anonymous), my app throws now

RuntimeException: Cookie jar has not been set. in /…/vendor/laravel/framework/src/Illuminate/Auth/SessionGuard.php:859

But how can there be a cookie jar for an unauthenticated request?

I feel like the addition of these two lines should not be here because they alter the test condition. If I remove them, then this test too fails with:

Failed asserting that exception of type "RuntimeException" matches expected exception "Illuminate\Auth\AuthenticationException". Message was: "Cookie jar has not been set." at…

@Boorinio WDYT about the scenario?

$guard->getSession()->shouldReceive('remove')->once();
$guard->getSession()->shouldReceive('get')->once()->andReturn(null);

$guard->authenticate();
Expand Down Expand Up @@ -313,6 +316,9 @@ public function testUserMethodReturnsCachedUser()
public function testNullIsReturnedForUserIfNoUserFound()
{
$mock = $this->getGuard();
$mock->setCookieJar($cookies = m::mock(CookieJar::class));
$cookies->shouldReceive('unqueue')->once();
$mock->getSession()->shouldReceive('remove')->once();
$mock->getSession()->shouldReceive('get')->once()->andReturn(null);
$this->assertNull($mock->user());
}
Expand Down