Impersonate kirby in route for authentication

I have a route which updates a page from a ajax form. It all works great when I am logged into the panel, but I guess it need to use $kirby->impersonate('kirby'); to authentic the logged out user?

How do I access $kirby inside the route?

  'routes' => [
        [
  'pattern' => 'release',
  'method' => 'POST',
  'action' => function () {
      $form = new \Uniform\Form([
    
        ]);

      // Perform validation and execute guards.
      $form->withoutFlashing()
          ->withoutRedirect()
          ->guard();

      if (!$form->success()) {
          // Return validation errors.
          return Response::json($form->errors(), 400);
      }
    

       
       $data = [
        'message' => get('message'),
    
      ];

      function addToStructure($page, $field, $data = array()){
        $fieldData = page($page)->$field()->yaml();
        $fieldData[] = $data;
        $fieldData = yaml::encode($fieldData);
        try {
          page($page)->update(array($field => $fieldData));
          return true;
        } catch(Exception $e) {
          return $e->getMessage();
        }
      }

      $kirby->impersonate('kirby');

      try {
      addToStructure(page('messages'), 'messages',[
        'date' => date("d-m-Y h:i:sa"),
        'message' => $data['message']
        ]);

      } catch (Exception $e) {

      }

      if (!$form->success()) {
          // This should not happen and is our fault.
          return Response::json($form->errors(), 500);
      }

      // Return code 200 on success.
      return Response::json([], 200);
  }

Use the kirby helper kirby() instead of $kirby. If you need to reference the kirby object more than once, you can store it in a variable:

$kirby = kirby();`

Whenever you are outsite of a template context (snippet, template), this is the way to access the Kirby instance.

ah magic! thanks so much :slight_smile: