Not able to set user in Auth

In CakePHP 4.0

When I’m trying to do $this->Auth->setUser($user), I’m getting error in
\cakephp\cakephp\src\Http\Session.php - Argument 1 passed to Cake\Http\Session::_overwrite() must be of the type array
protected function _overwrite(array &$old, array $new): void

Please help I can’t pass in an array as the $user is not a array

If user is not an array, what is it?

I’m going to guess it’s an entity. If so you can convert it to an array like this:

$arrayData = $userEntity->toArray();

I tried that too, but couldn’t to it.
Other help is appreciated.

you’re going to have to provide more information.

What is $user? What does your code look like around this problem area?

Can you provide some context? What is $user? Are you just trying to get the attributes of the logged in user? If so, and you’re using the authenticator plugin, then you just need this in your controller:

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

From there, you can iterate over $user and pull out whatever data you need, such as $user->username, $user->email, etc.

In a view or layout, you can grab a user attribute like this: