-1

I want to check if a user is logged in.

Because I need to access customer-specific information.

Thanks for your help.

Savan Patel
  • 2,348
  • 1
  • 16
  • 39

3 Answers3

4

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();
    }
}
Sumit
  • 4,875
  • 2
  • 19
  • 35
3

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
    }
  ?>

Felix Schönherr
  • 501
  • 2
  • 26
  • 1
    so I need this in my module's phtml file? –  Aug 14 '19 at 09:40
  • 1
    yes exactly, i hope i helped you fixing your problem – Felix Schönherr Aug 14 '19 at 09:40
  • 1
    thanks a lot my friend. very generous –  Aug 14 '19 at 09:40
  • So this is actually a bad answer? –  Aug 14 '19 at 09:45
  • I am sorry, I didn't know this – Felix Schönherr Aug 14 '19 at 09:46
  • its not bad, but we should avoid to use object manager directly in our files. – Sumit Aug 14 '19 at 09:51
  • 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 '19 at 09:52
  • @RahulAnand To what problems can this lead. i.e. security issues? –  Aug 14 '19 at 09:53
  • 2
    instead of obejct manager use a DI concept

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

    – Rahul Anand Aug 14 '19 at 09: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 '19 at 09:55
  • Do I need a seperate file for the DI or can I use it in the .phtml? –  Aug 14 '19 at 09:56
  • 1
    I faced one error on checkout page "undefined call to a method getId() on null object" just because I was calling model directly. When I replaced it with "API Interface" that error went away because DI loaded all the dependencies. – Rahul Anand Aug 14 '19 at 09:56
  • There is no need to define your own di (etc/di.xml, etc/frontend/di.xml and etc/adminhtml/di.xml) unless you want to override a Model, Controller, Block OR if you want to define events, plugins and virtual types.

    You can use the __construct() DI method to initialize your API interface and call the methods to do some operations. Calling API method will call model's method as implementation is mentioned in di.xml files for all modules.

    – Rahul Anand Aug 14 '19 at 09:58
  • @pave you need to define this in di.xml file and in __construct methods in your php files. you can't do this in phtml files without $objectManager, which is not a recommended approach. – Rahul Anand Aug 14 '19 at 10:01
  • ok I think I understand now. Thanks a lot for your help! –  Aug 14 '19 at 10:03
  • Cheers !! Happy to help – Rahul Anand Aug 14 '19 at 10:05
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;
        }
     }

}
Waqar Ali
  • 2,319
  • 16
  • 44