Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Support

Userdata is not set when logged in

Started by Robin Kählert 7 years ago · 4 replies · 415 views
7 years ago

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 [

JS
"username" => ""
"state" => "enabled"
"authenticated" => false
"authorized" => false
"language" => "en"

]

blueprints: Closure() {#278

TXT
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

TXT
#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
7 years ago

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:

YAML
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.

7 years ago

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?

7 years ago

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!

7 years ago

@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

Suggested topics

Topic Participants Replies Views Activity
Support · by Thomas, 1 week ago
2 64 17 hours ago
Support · by Anna, 3 days ago
2 68 20 hours ago
Support · by Justin Young, 21 hours ago
1 35 21 hours ago
Support · by Duc , 1 week ago
2 71 6 days ago
Support · by Colin Hume, 1 week ago
2 63 6 days ago