-1

I want to check if a user is logged in.

Because I need to access customer-specific information.

Thanks for your help.

1

3 Answers 3

3
+150

You can do this by making this following Code in your .phtml file:


<?php
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $customerSession = $objectManager->create("Magento\Customer\Model\Session");
    $contractAddress = $block->getContractAddress();

    if($customerSession->isLoggedIn()) {
        //Your Code
    }
  ?>

16
  • 1
    so I need this in my module's phtml file?
    – user82292
    Commented Aug 14, 2019 at 9:40
  • 1
    yes exactly, i hope i helped you fixing your problem Commented Aug 14, 2019 at 9:40
  • 2
    No pave, the answer is correct but magento recommend don't use Model object directly and also don't use "ObjectManager" directly. So we should avoid it in any other case except "Customer Session" Commented Aug 14, 2019 at 9:52
  • 2
    instead of obejct manager use a DI concept public function __construct( \Magento\Customer\Model\Session $customerSession ) { $this->customerSession = $customerSession; } Commented Aug 14, 2019 at 9:53
  • 2
    @pave, Calling a model object directly can raise error if all dependencies are not loaded which is needed by the model. DI concept says load API Interface and just because implementation of this interface is written in DI, it will load all the dependencies too without any fail. Commented Aug 14, 2019 at 9:55
4

You can check this with

class MyClass
{
    protected $customerSession;

    public function __construct(\Magento\Customer\Model\Session $customerSession) {
        $this->customerSession = $customerSession;
    }

    public function isCustomerLoggedIn() {
        return $this->_customerSession->isLoggedIn();
    }
}
4
  • where do I need to place this. I have no clue about magento
    – user82292
    Commented Aug 14, 2019 at 9:42
  • 2
    I think in your Block .php file Commented Aug 14, 2019 at 9:43
  • You can implement in Helper/Data.php or in Block class.
    – Sumit
    Commented Aug 14, 2019 at 9:45
  • It needs to be written in php file. Write it in your controller, Block, Helper files. Commented Aug 14, 2019 at 10:06
0

Try This

use Magento\Customer\Model\Customer;
use Magento\Customer\Model\CustomerFactory;
use Magento\Customer\Model\Session;

/**
 * Class Index
 */
class Index extends \Magento\Framework\App\Action\Action
{

/**
 * @param \Magento\Framework\App\Action\Context
 * @param Customer
 */
public function __construct(
    \Magento\Framework\App\Action\Context $context,
    Session $customerSession,
) {
    $this->customerSession   = $customerSession;
    parent::__construct($context);
}
public function execute()
{
            if ($this->customerSession->isLoggedIn()) {
                $this->messageManager->addWarning('User is loggedIn');
                return $resultRedirect;
            }
        } else {
            $this->messageManager->addWarning('User Not loggedIn');
            return $resultRedirect;
        }
     }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.