How to get current logged in customer data in Magento 2

While working on Magento several times we need to get the details of current logged in customer. It can easily be checked whether the customer is logged in or not. If the customer is logged in then we can retrieve its data and use it for our functionality implementation.

Magento uses session to store current logged in customer. We can use the Magento\Customer\Model\Session class to get logged in customer. Below is the code snippet which will return the customer data if customer is logged in.


namespace WebbyTroops\Customer\Block;

class Customer extends \Magento\Framework\View\Element\Template
     * @var \Magento\Customer\Model\Session
    protected $_customerSession;

     * @param \Magento\Framework\View\Element\Template\Context $context
     * @param \Magento\Customer\Model\Session $customerSession
     * @param array $data
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Customer\Model\Session $customerSession,
        array $data = []
    ) {
        $this->_customerSession = $customerSession;
        parent::__construct($context, $data);

     * Get logged in Customer
    public function getLoggedInCustomer()
        if ($this->_customerSession->isLoggedIn()) {
            return $this->_customerSession->getCustomer();
        return false;

Get Logged In Customer in template file

Now you can easily access the logged in customer data in you phtml file.

$customer = $block->getLoggedInCustomer();
if ($customer) {
    $email = $customer->getId();
    $email = $customer->getEmail();
    $firstName = $customer->getName();

If you want to get logged in customer in the REST API by bearer token then follow this article.

Leave A Comment

Go To Top