Check Whether the user is logged in drupal 8

  This will check the whether the user is logged or not. 

  $anonymous = \Drupal::currentUser()->isAnonymous();
   if($anonymous) {
     drupal_set_message(t('Please login to Apply.'), 'error');
   }