4

I'am trying to check whether a user is logged-in or not; with \Drupal::currentUser()->id() I am getting the user ID for the anonymous user (i.e. 0), even though I'm logged in.

How can I resolve this?

I'm just calling this function in the .module file, and it is not run from cron. I am using the following code.

use Drupal\Core\Session\UserSession;
use Drupal\Core\Session\AccountInterface;

$user = \Drupal::currentUser();
if ($user->isAuthenticated()) {
  echo 'logged-in';
}
else {
  echo 'guest';
}

enter image description here

  • This seems like an odd issue, I'm wondering if showing some code would help figure out what's going on. – Weboide Jun 29 '16 at 17:18
  • What is the context in which you are calling the currentUser method? Is it inside of a hook? If so, what hook? – Eyal Jun 29 '16 at 18:25
  • Please show the function containing that code, or we are just going to guess. – kiamlaluno Jun 30 '16 at 6:32
  • 1
    Which function you put the code? – Jimmy Ko Jul 21 '16 at 12:50
3

To determine if a user is logged-in (authenticated), it would be best to use:

\Drupal::currentUser()->isAuthenticated()

  • 1
    the \Drupal::currentUser() is returning the anonymous user object, therefore, isAuthenticated() function returns false.Well the main thing is why \Drupal::currentUser() is returning anonymous user when i'm logged-in as admin. – user1960 Jun 29 '16 at 12:30
2

It seems you are running your code directly from your .module file and not inside a function. This way your code may not work because most services are likely to be not instantiated at the time the module files are included.

Try to run your code from a hook (e.g. hook_entity_view()) or use Devel module's Execute PHP-Code functionality (at /devel/php).

1

I encountered this kind of behaviour when using hook_download_file. However in my situation I had varnish to pass request to Drupal backend and I were downloading file through Drupal. Varnish checked that it's actually file and stripped out cookies that contains session info.

0

Try

\Drupal\user\Entity\User::load(\Drupal::currentUser()->id())->uid->value
  • it isn't working either. It displays The website encountered an unexpected error. Please try again later. – user1960 Jun 29 '16 at 12:05
  • @user1960 where do you test this? in module, in preprocess hook? where? – Yusef Mohamadi Jun 29 '16 at 12:07
  • in .module file and in module controller class. – user1960 Jun 29 '16 at 12:09
0

Try this.

use Drupal\Core\Session\AccountInterface;

function mymodule_function() {
  $account = \Drupal::currentUser();
  if ($account->isAuthenticated()) {
    // User is logged in.
  }
}

Refer the below Link for more details

https://www.drupal.org/node/2017231

0

For the sake of more robust and descriptive code, you can make use of the handy User::isAnonymous() method, e.g.

if (\Drupal::currentUser()->isAnonymous()) {
  // Anonymous user...
}

or

$user = \Drupal::currentUser();

if ($user->id() == 0) {
    // is visitor
}
else {
    // is logged
}
0

It appears that the code in the original post is not in any function, it's just in the global spec of whichever file it's in, so the current user probably hasn't been loaded yet.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.