If I execute this in the controller of my logout page:
user()->logout();
The following command returns still true in the page template (and user object exists):
user()->isLoggedIn();
User is logged out successfully though, but the logout page is rendered as if the user is still logged in. Tested in Kirby 3.2.5.
This used to work correctly before, but maybe the inner workings of the logout operation were changed in some update. If so, is it intentional that logout is not committed within the same page request?
<?php
if ($user) {
if ($user->isLoggedIn() == true)
echo 'User is logged in';
else
echo 'User is logged out';
} else {
echo "No user object";
}
?>
When I enter the page the first time while logged in, I get “User is logged in”, which shouldn’t be the case. The second time I get “No user object”, which is correct. Can you repeat the scenario and confirm if you get the same result?
Well, technically, that equals calling $user->logout() first and then setting $user = null, since logout has return type of void.
The thing is that I have a bunch of snippets, which help rendering each page, and the content of these snippets vary depending on whether the user is logged in or not. While it’s technically possible to piggy-bag the $user reference to each part responsible for rendering the page, I find it counter-intuitive that kirby()->user() cannot be trusted in this specific instance to reflect the current situation.
Also, I’m quite sure this used to work before. That’s why I’m wondering whether this is a regression bug or a deprecated feature.
Sorry, I forgot to add one critical line to the template, no idea how that happened… So the template I used in the reproducable error was
site/templates/logout.php
<?php
$user = kirby()->user();
if ($user) {
if ($user->isLoggedIn() == true)
echo 'User is logged in';
else
echo 'User is logged out';
} else {
echo "No user object";
}
?>
So the issue is that, even though I could follow your advice and assign $user to null in controller, and use that instead of kirby()->user(), any snippet or plugin in the template using kirby()->user() will still think user is logged in.
And I was able to confirm that this issue did not exist in Kirby version 3.1.4, so I think it’s worthwhile to submit the issue to Github.