Restrict content to a user role in a template file I want to restrict a single node to anonymous users in Drupal 8. I don't want to use any module for it as its useless for single restriction as per me.

I know that I can do it with something like this.

{% if user.hasPermission('myPermission') %}
     //my template HTML
{% else %}
    //some login for the customer
{% endif %} 


I don't want to restrict the content via permission, instead I want to restrict it with something like 

{% if user.hasRole('authenticated') %}


Is this possible with a theme hook or do I have to use a module?
To check in twig if the user is authenticated you can use the default variable logged_in:

{% if logged_in %}
  &lt;p&gt;The user is authenticated".&lt;/p&gt;
{% endif %}


This works because the user module provides the variable in preprocess for all templates:

/**
 * Implements hook_template_preprocess_default_variables_alter().
 *
 * @see user_user_login()
 * @see user_user_logout()
 */
function user_template_preprocess_default_variables_alter(&amp;$variables) {
  $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();
}


Generic roles

If you want to check a generic role, you can't use hasRole(), because this method is not available for accounts. You would have to use getRoles() and check if the role is in the returned array:

{% if 'example_role' in user.getroles  %}
  &lt;p&gt;The user has the role "example_role".&lt;/p&gt;
{% endif %}