Display the data of a logged in user

Hello everyone, I am using cakephp 3.8 and would like to display the name of the logged in user in the default.ctp. How do I go about this because $this->request->session() ->read(‘Auth.User.name’) gives no results.
Thank you all.

This is how I do it in CakePHP 4.1:

$user = $this->request->getAttribute('identity');

Then, you can access any of the user table fields with $user->FIELDNAME, for example, $user->email

Hi @yvesroma190,

As you’ve said you’re using CakePHP 3.8, I’m assuming you are using AuthComponent to handle authentication. You can get logged in user’s data via $this->Auth->user('name');. It will return only data that you’ve set while logging user in $this->Auth->setUser($user).

I suggest you give https://book.cakephp.org/3/en/controllers/components/authentication.html link a read from where you can get reference.

Thank you all for help