Restrict content to a user role in a template file
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 %}