Problems when setting up restricted page access (Cookbook recipe)

I’m trying to restrict access to a specific template and set up a simple login page. I’m following the recipe in the Kirby Cookbook.

I followed the recipe exactly and did the following:

However, when trying to access http://my-website.com/login, I get this error:

Like I said, I literally just copied the files from the cookbook, so I’m having a hard time figuring out where I went wrong. Can someone help me out?

Additional info:

  • I’m developing on a local machine (MAMP + CodeKit)
  • I’m working with the latest Kirby release (3.5.5)

Is there a stack trace below this message that tells us where the code goes wrong?

Thanks for the quick reply. There is:


Ok, it refers to the esc() helper on line 12. What is the email address that is passed to that function?

Well… none? Since I don’t even get to the login form? First I thought the browser might be passing a saved e-mail but even with a fresh browser, cleared cache & cookies and all saved logins removed the error shows up.

Again, this is »my« code for the controller:

<?php

return function ($kirby) {

  // don't show the login screen to already logged in users
  if ($kirby->user()) {
    go('/');
  }

  $error = false;

  // handle the form submission
  if ($kirby->request()->is('POST') && get('login')) {

    // try to log the user in with the provided credentials
    try {
      $kirby->auth()->login(get('email'), get('password'));

      // redirect to the homepage if the login was successful
      go('/');
    } catch (Exception $e) {
      $error = true;
    }

  }

  return [
    'error' => $error
  ];

};

The error happens in the template and I know now what the issue is. The ecs() helper expects a string, and if get('email') or get('password') is null, the error is thrown.

So this code should be changed to

value="<?= get('email') ? esc(get('email'), 'attr') : '' ?>">

And same for the password.

Thanks, that did the trick!