Adding the following snippet in your theme’s functions.php file will let you redirect if you are not logged in WordPress.