WordPress beginner here. I'm trying to check if a shortcode exists in the page being requested by the user, then if it does, redirect if the user is not signed in. 

    function redirect_to_home() {
      if (has_shortcode(get_the_content(), 'shortcode')) {
        if(!is_admin() && !is_user_logged_in()) {
          //redirect
          exit();
        }
      }
    }
    add_action('template_redirect', 'redirect_to_home');

Is this close? And currently this code is placed in the functions.php files, is this correct?