11

How can I check whether user is logged in or not in Magento 2.0 I using phtml file I want to check user logged in or not.

<span class="selllink switcher top-links">
    <span class="" data-bind="scope: 'customer'">
        <span data-bind="text: customer().fullname ? $t('Welcome,%1').replace('%1', customer().firstname) : '<?=$block->escapeHtml($welcomeMessage) ?>'"></span>
    </span>
    <script type="text/x-magento-init">
    {
        "*": {
            "Magento_Ui/js/core/app": {
                "components": {
                    "customer": {
                        "component": "Magento_Customer/js/view/customer"
                    }
                }
            }
        }
    }
    </script>
</span>
Siarhey Uchukhlebau
  • 15,957
  • 11
  • 54
  • 83
vnnogile_user
  • 385
  • 3
  • 4
  • 17

7 Answers7

23

To check if customer is loged in or not you can call Magento\Customer\Model\Session::isLoggedIn() method.

If you want to do that properly in *.phtml files you should use helper inside the template. Object manager is not preferred way to call models inside template files. So I am not recommending to follow this post There isn't core helper that can be reused so you need to create new one.

<?php
namespace YourCompany\ModuleName\Helper;

class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
    public function __construct(
        \Magento\Framework\App\Helper\Context $context,
        \Magento\Customer\Model\Session $customerSession
    ) {
        $this->customerSession = $customerSession;
        parent::__construct($context);
    }

    public function isLoggedIn()
    {
        return $this->customerSession->isLoggedIn();
    }
}

Then in your *.phtml file you can call your helper and call isLoggedIn method like this:

<?php $helper = $this->helper('YourCompany\ModuleName\Helper\Data'); ?>
<?php if($helper->isLoggedIn()) : ?>
    logged in
<?php else : ?>
    not logged in
<?php endif; ?>
Miroslav Petroff
  • 1,842
  • 1
  • 16
  • 22
5

This is for only Magento 2.0, add this in phtml file:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManager->create('Magento\Customer\Model\Session');

Check via following:

if($customerSession->isLoggedIn()){
   // your code
}
Siarhey Uchukhlebau
  • 15,957
  • 11
  • 54
  • 83
Ravi Thanki
  • 426
  • 4
  • 13
4

If you want to get user logged in at template level phtml, you can call :

<?php $_loggedin = $this->helper('Magento\Checkout\Helper\Cart')->getCart()->getCustomerSession()->isLoggedIn(); ?>
Afham
  • 366
  • 2
  • 7
2

You can also check with below code

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManager->get('Magento\Framework\App\Http\Context');
$isLoggedIn = $customerSession->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH);

    if($isLoggedIn){
        // Logged In
    }else{
        // Not Logged In
    }
Jackson
  • 9,909
  • 29
  • 128
  • 217
0

You must call object in magento 2 for custom coding in any page. so code will look like this

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManager->get('Magento\Customer\Model\Session');

if($customerSession->isLoggedIn()){

//Code

}
Qaisar Satti
  • 32,469
  • 18
  • 85
  • 137
0

I am try to get the logged in condition on header.phtml file with above helper code in custom module app/code/venter/ module name/ helper/Data.php Actually my concerns is thats i want to restrict all the categories products page cms page from the frontend if any frontend users are not logged in it will not access any pages except home page.where we apply the code.please anyone help me

class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
public function __construct(
    \Magento\Framework\App\Helper\Context $context,
    \Magento\Customer\Model\Session $customerSession
) {
    $this->customerSession = $customerSession;
    parent::__construct($context);
}

public function isLoggedIn()
{
    return $this->customerSession->isLoggedIn();
}

}

dev tester
  • 123
  • 7
-7
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManager->get('Magento\Customer\Model\Session');


if($customerSession->isLoggedIn()) {
    // do your code
}
Abdul
  • 9,701
  • 1
  • 20
  • 43
Rushvi
  • 2,843
  • 2
  • 14
  • 31
  • It's working but username not showing on aws. @RJ07 – vnnogile_user Jul 13 '16 at 09:45
  • Why this is marked as solution when it's not working. And again creating new instance of the object manager in template file. Check out my answer and the comments below. Other thing is that you are trying to display the username with knockoutJS variable which is out of the question scope. – Miroslav Petroff Jul 18 '16 at 11:37
  • We should never use direct object managers in any class. – Sarvesh Tiwari Jun 06 '21 at 12:46