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

4
  • This seems like an odd issue, I'm wondering if showing some code would help figure out what's going on.
    – Weboide
    Commented Jun 29, 2016 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
    Commented Jun 29, 2016 at 18:25
  • Please show the function containing that code, or we are just going to guess.
    – avpaderno
    Commented Jun 30, 2016 at 6:32
  • 1
    Which function you put the code?
    – Jimmy Ko
    Commented Jul 21, 2016 at 12:50

8 Answers 8

3

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

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

1
  • 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
    Commented Jun 29, 2016 at 12:30
3

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

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

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
3
  • it isn't working either. It displays The website encountered an unexpected error. Please try again later.
    – user1960
    Commented Jun 29, 2016 at 12:05
  • @user1960 where do you test this? in module, in preprocess hook? where?
    – Yuseferi
    Commented Jun 29, 2016 at 12:07
  • in .module file and in module controller class.
    – user1960
    Commented Jun 29, 2016 at 12:09
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.

1
0

I ran into the same issue in some code today. The code was in a controller and the controller was mapped to a route for a URL which was being accessed by users from emails.

The problem was that the URLs in the emails had http and the site was running on https. The site was also accessible via http and was not automatically redirecting to https.

So when the user comes to the site from the email he would be on http and the session would be an anonymous session.

Hope this helps somebody!

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