Check if the user is logged-in in a theme
If you just to check the current user is logged-in, you can use $variables['logged_in'], which is generally available in all the template files.

For example, the mark.html.twig file uses the following code, although the only documented variable is status.

{% if logged_in %}
  {% if status is constant('MARK_NEW') %}
    &lt;span class="marker"&gt;{{ 'New'|t }}&lt;/span&gt;
  {% elseif status is constant('MARK_UPDATED') %}
    &lt;span class="marker"&gt;{{ 'Updated'|t }}&lt;/span&gt;
  {% endif %}
{% endif %}


The variable is explicitly documented in other template files, such as html.html.twig, page.html.twig, and node.html.twig.

The variable is available in all the template files, since it is initialized in _template_preprocess_default_variables() that invokes user_template_preprocess_default_variables_alter() (an implementation of hook_template_preprocess_default_variables_alter()), which contains the following code.

  $user = \Drupal::currentUser();

  $variables['user'] = clone $user;
  // Remove password and session IDs, since themes should not need nor see them.
  unset($variables['user']-&gt;pass, $variables['user']-&gt;sid, $variables['user']-&gt;ssid);

  $variables['is_admin'] = $user-&gt;hasPermission('access administration pages');
  $variables['logged_in'] = $user-&gt;isAuthenticated();


_template_preprocess_default_variables() is called by template_preprocess(), which is the function called for theme hooks implemented as templates; this guarantees the variable is available in all the template files.

Keep in mind that macros don't have access to the current template variables, so trying to access logged_in in the code of a macro would not have any effect.
Between the template files used from Drupal core modules, the ones using a macro are:


menu.html.twig

{% macro menu_links(items, attributes, menu_level) %}
  {% import _self as menus %}
  {% if items %}
    {% if menu_level == 0 %}
      &lt;ul{{ attributes }}&gt;
    {% else %}
      &lt;ul&gt;
    {% endif %}
    {% for item in items %}
      &lt;li{{ item.attributes }}&gt;
        {{ link(item.title, item.url) }}
        {% if item.below %}
          {{ menus.menu_links(item.below, attributes, menu_level + 1) }}
        {% endif %}
      &lt;/li&gt;
    {% endfor %}
    &lt;/ul&gt;
  {% endif %}
{% endmacro %}

book-tree.html.twig

{% macro book_links(items, attributes, menu_level) %}
  {% import _self as book_tree %}
  {% if items %}
    {% if menu_level == 0 %}
      &lt;ul{{ attributes }}&gt;
    {% else %}
      &lt;ul&gt;
    {% endif %}
    {% for item in items %}
      &lt;li{{ item.attributes }}&gt;
        {{ link(item.title, item.url) }}
        {% if item.below %}
          {{ book_tree.book_links(item.below, attributes, menu_level + 1) }}
        {% endif %}
      &lt;/li&gt;
    {% endfor %}
    &lt;/ul&gt;
  {% endif %}
{% endmacro %}

menu--toolbar.html.twig

{% macro menu_links(items, attributes, menu_level) %}
  {% import _self as menus %}
  {% if items %}
    {% if menu_level == 0 %}
      &lt;ul{{ attributes.addClass('toolbar-menu') }}&gt;
    {% else %}
      &lt;ul class="toolbar-menu"&gt;
    {% endif %}
    {% for item in items %}
      {%
        set classes = [
          'menu-item',
          item.is_expanded ? 'menu-item--expanded',
          item.is_collapsed ? 'menu-item--collapsed',
          item.in_active_trail ? 'menu-item--active-trail',
        ]
      %}
      &lt;li{{ item.attributes.addClass(classes) }}&gt;
        {{ link(item.title, item.url) }}
        {% if item.below %}
          {{ menus.menu_links(item.below, attributes, menu_level + 1) }}
        {% endif %}
      &lt;/li&gt;
    {% endfor %}
    &lt;/ul&gt;
  {% endif %}
{% endmacro %}



For example, changing the last macro with the following code would not have the expected result.

{% macro menu_links(items, attributes, menu_level) %}
  {% import _self as menus %}
  {% if items %}
    {% if menu_level == 0 %}
      &lt;ul{{ attributes.addClass('toolbar-menu') }}&gt;
    {% else %}
      &lt;ul class="toolbar-menu"&gt;
    {% endif %}
    {% for item in items %}
      {%
        set classes = [
          'menu-item',
          logged_in ? 'menu-item--logged-in-user',
          item.is_expanded ? 'menu-item--expanded',
          item.is_collapsed ? 'menu-item--collapsed',
          item.in_active_trail ? 'menu-item--active-trail',
        ]
      %}
      &lt;li{{ item.attributes.addClass(classes) }}&gt;
        {{ link(item.title, item.url) }}
        {% if item.below %}
          {{ menus.menu_links(item.below, attributes, menu_level + 1) }}
        {% endif %}
      &lt;/li&gt;
    {% endfor %}
    &lt;/ul&gt;
  {% endif %}
{% endmacro %}