8.5.x Drupal.php Drupal::currentUser()
8.0.x Drupal.php Drupal::currentUser()
8.1.x Drupal.php Drupal::currentUser()
8.2.x Drupal.php Drupal::currentUser()
8.3.x Drupal.php Drupal::currentUser()
8.4.x Drupal.php Drupal::currentUser()
8.6.x Drupal.php Drupal::currentUser()

Gets the current active user.

Return value



core/lib/Drupal.php, line 246
Contains \Drupal.


Static Service Container wrapper.


public static function currentUser() {
  return static::getContainer()


taggartj’s picture

$uid = \Drupal::currentUser()->id();

TristanTheKnightAway’s picture

This worked for me and temporarily stopped my madness.

pownraj’s picture

To access the user object in twig, just use

{% user.id %}

Like mentioned in the docs for nodes, user variable also available but with few restrictions.

Only method names starting with "get", "has", or "is" and a few common
 *   methods such as "id", "label", and "bundle" are available.
jmsosso’s picture

If you want to get the current user from a controller use dependency injection:

class MyController extends ControllerBase {

   * Drupal\Core\Session\AccountProxy definition.
   * @var \Drupal\Core\Session\AccountProxy
  protected $currentUser;

   * {@inheritdoc}
  public function __construct(AccountProxy $current_user) {
    $this->currentUser = $current_user;

   * {@inheritdoc}
  public static function create(ContainerInterface $container) {
    return new static(

Rob230’s picture

FYI you don't need to do this because $currentUser is already injected into ControllerBase (although it's protected, you have to use the method: https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Controlle...

However, this is useful advice for anyone needing to inject $currentUser into a class that doesn't extend from ControllerBase.

joegl’s picture

I found it important to keep in mind this method only returns an AccountProxyInterface object and not a full UserInterface object. To get access to the full UserInterface object, load the user using the uid of the currentUser:

$current_user = \Drupal::currentUser();
$user = \Drupal\user\Entity\User::load($current_user->id());

// or just
$user = \Drupal\user\Entity\User::load(\Drupal::currentUser()->id());

There may definitely be a better method out there.

vlad13’s picture

Is there the proper way to set currentUser? I have tried to authenticate user with $uid = \Drupal::service('user.auth')->authenticate($username, $password); but currentUser is empty.

AccountProxy::setAccount() is deprecated.

Works only account switching:

  $user = \Drupal\user\Entity\User::load($uid);

  $accountSwitcher = \Drupal::service('account_switcher');