How to check if customer is logged in or not in magento 2?

In Magento 2 we can check Customer is logged in or not by below way,
Using Block file, Pass Magento\Framework\App\Http\Context as a dependency to construct method.


namespace Rbj\Customer\Block;

class Customerinfo extends \Magento\Framework\View\Element\Template
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Framework\App\Http\Context $httpContext,
        array $data = []
    ) {
        $this->httpContext = $httpContext;
        parent::__construct($context, $data);

     * return bool
    public function getLogin() {
        return $this->httpContext->isLoggedIn();

Call getLogin function in template file,

$isLoggedin = $block->getLogin();
if($isLoggedin) {
    // show your message

Using ObjectManager, Never use Objectmanager directly in real code in magento 2. Here I have show just demonstrate using Objectmanager. Using Objectmanager directly create performance issue for the site.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$userContext = $objectManager->get('Magento\Framework\App\Http\Context');
$isLoggedIn = $userContext->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH);
if($isLoggedIn) {
// do coding for customer loggin