Drupal 8 check if user is logged in

Check if a user is logged in in drupal 8. Snippet:

if (\Drupal::currentUser()->isAuthenticated()) {
  // This user is logged in.
} else {
  // This user is not logged in.
}

 

 

Saved you some valuable time?

Buy me a drink 🍺 to keep me motivated to create free content like this!