Userdata is not set when logged in

Hi together,

i am trying to access the grav.user object to check if the visitor of the website is logged in. Unfortunately this object is not filled with the correct data. E.g. username is empty.

Dumping the content of grav.user gives me the following:

Grav\Common\User\User {#279 #_media: null #gettersVariable: "items" #items: array:5 [ "username" => "" "state" => "enabled" "authenticated" => false "authorized" => false "language" => "en" ] #blueprints: Closure() {#278 class: "Grav\Common\User\DataUser\UserCollection" this: Grav\Common\User\DataUser\UserCollection {#135 …} file: "/mnt/work/animal_equality/git/lp-de/system/src/Grav/Common/User/DataUser/UserCollection.php" line: "57 to 61" } #storage: Grav\Common\File\CompiledYamlFile {#276 #filename: "/mnt/work/animal_equality/git/lp-de/user/accounts/.yaml" #handle: null #locked: null #extension: ".yaml" #raw: "" #content: [] #settings: [] } #nestedSeparator: "." }

I have not found anything in the docs which gives a hint why this could happen. Would be great if someone could help me out.

Thanks a lot!

1 Like

Hi @sque, Are you sure a user did indeed login? If not, there is no data about a user…

To force the user to login when accessing a page, add the following to the page:

access:
    site.login: true

See Create a private area for more information.

If a user tries to access that page, a login form will be presented. After the user logged in, the user object will show the correct data. If your user logs-out (right top corner of page), the user object will be empty again.

Hope this helps.

Hi @pamtbaau thank you very much for your fast response. Indeed i am not sure. Currently a question from my side arises:

Actually i want to check if an admin is logged in. Is there a difference between admin and user login?

I added the access requirement to the page and indeed even if i logged in as an administrator to the backend, it still asks me for login. So it looks like i am not logged in and thats the reason.

Then my question would be: how can i check in a theme template if the user is logged in to the admin plugin?

Ha i think i got it. The setting Session -> Initialize was set to false. After setting it to true the userdata is set correctly.

Thank you very much for pointing me into the right direction @pamtbaau!

@sque, While reading the README of the login plugin for your last question, I noticed the following quote:

Note: the frontend site and admin plugin use different sessions so you need to explicitly provide a login on the frontend.

This might answer your question about the difference between the admin login and user login.

1 Like