Q:

Getting user id of logged in user in component

I'm making my first component and am trying to get the id of the logged-in user and request some data from the model based on that user id.
If I search around this would return the current user:
use Joomla\CMS\Factory;
$user = Factory::getUser();

But $user returns as an empty object:
Joomla\CMS\User\User Object ( [isRoot:protected] => [id] => 0 [name] => [username] => [email] => [password] => [password_clear] => [block] => [sendEmail] => 0 [registerDate] => [lastvisitDate] => [activation] => [para.......

I'm currently doing this in my view.html.php right before $this->items = $this->get('Items'); is called. Could it have something to do with the place where im caling getUser()?

A:

You are doing it right but your user object is not empty.
[id] => 0 because the current user is not logged in.
Try to login and do something like:
use Joomla\CMS\Factory;

$user = Factory::getUser();
if ($user->id > 0)
{
   echo 'Hello ' . $user->name;
}

