Get currently logged in user I am facing an almost similar problem whereby if I do:

$userCurrent = \Drupal::currentUser();
$name = $userCurrent-&gt;getUsername(); 


I get the site account name instead of the logged in user. Which is the right way to get the logged in username or roles for that matter. I even tried:

$user = Drupal\user\Entity\User::load($userCurrent-&gt;id());


But all this does not seem to work.

Any suggestions?
Try this

&lt;?php
// Load the current user.
$user = \Drupal\user\Entity\User::load(\Drupal::currentUser()-&gt;id());

// get field data from that user
$website = $user-&gt;get('field_website')-&gt;value;
$body = $user-&gt;get('body')-&gt;value;


$email = $user-&gt;get('mail')-&gt;value;
$name = $user-&gt;get('name')-&gt;value;
$uid= $user-&gt;get('uid')-&gt;value;
?&gt;


Or

$uid = \Drupal::currentUser()-&gt;id();
$user = \Drupal\user\Entity\User::load($uid);
$name = $user-&gt;getUsername();


You will get more details about User::getUsername.