Kirby 4 - check if a user is authenticated / logged in
In todays note to self, its just a basic reminder of how to check if a user is logged in or not.
Mainly because I've looked for this at least twice, and got stuck with old forum answers with pre-Kirby v.3 syntax that didn't work.
As with most stuff in Kirby it's very easy once you know how. But sometimes you (me) still get stuck in docs and google searching for small things like this.
if ($kirby->user()) {
/* Do some cool stuff*/
}
Or if you maybe just want to hide the whole page behind a login:
/* in top of the master template file */
<?php if (!$kirby->user()) redirect('/panel') ?>
I use this to be able to make new sections of a page without it being open to the public. For example when I want the client or my coworker to just quickly have a look at it before releasing it.
I also use the redirect solution for working on a WIP-project until it's ready to go live.