Check for logged-in user from within router plugin

Is there something I don’t see why this is not working? Trying to limit a route defined in a plugin to logged-in users only:

Kirby::plugin('my/plugin', [
    'routes' => [
        [
            'pattern' => 'protectedpage',
            'action'  => function () {
                if ( kirby()->user() ) :
                    // do stuff

The condition evaluates as false, even while I am logged in, as kirby()->user() is null - as the error message tells me when trying kirby()->user()->isLoggedIn()

Thank you for your help!

That code should actually work. However, if you want to call isLoggedIn() (why? not needed I think), then you should make sure you have a user object first:

if (($user = kirby()->user()) && $user->isLoggedIn()) {
  // do stuff
}

Thank you, all good: the error was caused by something else - if (kirby()->user()) works as it should. And indeed, isLoggedIn() is not needed, I was just trying different things (instead of looking at where the real problem was :wink: )