# Impersonate kirby in route for authentication

**URL:** <https://forum.getkirby.com/t/impersonate-kirby-in-route-for-authentication/20187>\
**Category:** Questions\
**Tags:** v3\
**Created:** [November 28, 2020, 4:14pm UTC](https://forum.getkirby.com/t/impersonate-kirby-in-route-for-authentication/20187 "2020-11-28T16:14:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![\_gareth\_j](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/_gareth_j/32/4694_2.png) [@\_gareth\_j](https://forum.getkirby.com/u/_gareth_j)\
**Post date:** [November 28, 2020, 4:14pm UTC](https://forum.getkirby.com/t/impersonate-kirby-in-route-for-authentication/20187/1 "2020-11-28T16:14:05Z")

</div>

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);
  }

```

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [November 28, 2020, 4:24pm UTC](https://forum.getkirby.com/t/impersonate-kirby-in-route-for-authentication/20187/2 "2020-11-28T16:24:07Z")

</div>

> [@\_gareth\_j](#):
>
> How do I access $kirby inside the route?

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:

```auto
$kirby = kirby();`

```

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

---

<div class="post-metadata">

**Author:** ![\_gareth\_j](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/_gareth_j/32/4694_2.png) [@\_gareth\_j](https://forum.getkirby.com/u/_gareth_j)\
**Post date:** [November 28, 2020, 4:32pm UTC](https://forum.getkirby.com/t/impersonate-kirby-in-route-for-authentication/20187/3 "2020-11-28T16:32:37Z")

</div>

ah magic! thanks so much 🙂
