I want to check if a user is logged in.

Because I need to access customer-specific information.

Thanks for your help.


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

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

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

  • 1
    so I need this in my module's phtml file? – Pave Aug 14 at 9:40
  • 1
    yes exactly, i hope i helped you fixing your problem – felix Aug 14 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" – Rahul Anand Aug 14 at 9:52
  • 2
    instead of obejct manager use a DI concept public function __construct( \Magento\Customer\Model\Session $customerSession ) { $this->customerSession = $customerSession; } – Rahul Anand Aug 14 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. – Rahul Anand Aug 14 at 9:55

You can check this with

class MyClass
    protected $customerSession;

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.