Get active frontend user in page hook

hi everyone,

i am facing the following problem: i am trying to save the last modification made by a front end user via a page.update:after hook. my code:

'page.update:after' => function ($newPage, $oldPage) {
      $newPage->update([
        'dateModified' => time(),
        'modifiedByUser' => kirby()->user()->name()
      ]);
    },

dateModified is working fine for panel and frontend users, since it uses only the time() function. but for the user name i am having problems. panel users get logged via the hook, but kirby()->user()->name() seems to not be working for frontend users. any idea on how to reach my goal? :confused:

But your frontend users are logged in users, just without Panel access?

hi sonja, yes, let me clarify. the frontend users dont have panel access. with ‘logged in users’ you mean if they appear in the users view in the panel? yes I manage them from there.

i tried a workaround, but with no success because it overrides the value:

after i save the page from the frontend, logged in as a frontend user, i save also the username (modifiedByUser) directly via the $page->update() function (along with other fields). but since the hook gets called after the page is saved, the value i save gets overriden by kirby()->user()->name() in the hook. if i remove

'modifiedByUser' => kirby()->user()->name()

from of the hook everything works from the frontend and the value is saved (because the hook does not overwrite the update() value), but then the panel users dont get logged, because the $page->update() happens obviously only in the frontend. I hope i explained it clear enough

I have no answer to your question, but I stumbled across this.

Wouldn’t it be safer to use

'modifiedByUser' => kirby()->user()->id();

because usually usernames can change.

hm yes probably, but in our case the chance that they change are really small. it is more of a quick overview and if I use IDs then the editors would have to check who that was in their team.

I solved this. My mistake was:

  • i was logging in as a frontend user
  • right before i call $page->update($data) i was calling impersonate(‘kirby’)
  • the user was getting changed of course, due to this
  • the logs were showing therefore an empty name

by removing the impersonate() everything is working fine now :slight_smile: was a dumb copy paste mistake since i was using code from the cookbook.