How to get current user details in drupal 8

By Arun AK, 21 May, 2017

In drupal 8 you can get current logged in users info by using currentUser() method.

If you want to use it in module file or other than a class you can call this method like below

$currentAccount = \Drupal::currentUser();

In class files inside class you can use

$currentAccount = $this->currentUser();

  1. To get User ID$currentAccount->id();
  2. To get User Name$currentAccount->getUsername();