Remember login after login($password)


I’m creating a login form on my Kirby-powered website in which users login, I’m having trouble understanding though how to get all pages to remember that the user logged-in.

Using the sample in the instructions:

$user = $site->user($userName)
if($user and $user->login($userPassword))

I’m checking if the login details are correct, but what would I do after that?

You only need that part to log the user in, e.g. after submitting a login form. Once the user is logged in, Kirby finds that automatically. You can find a guide on this here:

To check if a user is logged in e.g. to show some parts of your website only to logged-in users, you can simply check like this:

<?php if($site->user()): ?>
Top Secret: the meaning of life is…
<?php endif ?>

Or at the top of pages that you want only logged-in users to access:

<?php if(!$site->user()) go('/') ?>
1 Like

Ah gotcha, that’s fantastic! Thanks :smile:

Another question; is there an easy way to create a registration form for new users outside of the panel? Or would I have to code that by hand, and place a new file for the users in the foldeR?

You have to create and style the form yourself, but to actually register the user, you can use $site->users()->create(), which will create the user file for you.