1

I need to know if the customer has successfully logged in or logged out in the current request. I need to do it in a After plugin (after execute of LogIn and Logout from customer) How can I check it? Is there something in request I could get and compare with data in login's logout's afterExecute? I though about formkey, but I'm not 100% sure it changes when user toggle between login and logout.

Update (Clarifying the question):

I'm developing a module for login and logout using AJAX but I don't want to reload the page in any moment, this is why I need to add a extra piece of HTML, this HTML is the header links (that first menu in Luma theme, with Register and login when are not logged, and account, address, wishlist etc when logged). Once the menu changes when toggled between login and logout I need to update it (in my case replace the whole menu).
So I created a after plugin used for both actions in customer module: Login and Logout, once they do the same thing I use same plugin for both action (but it doesn't matter). The login may fail so I don't need to updated the menu, then I don't want to send HTML attached to answer unnecessarily, this is why I need to know if the customer was logged out and now logged in and vice-versa.

0

You could create a customer unique cached item with a TTL within your "just logged in" range. For example an observer would create a cached item with a TTL of 5 mins unique to the customer after login. Then to find out if the customer logged in within the last 5 mins your request would simply check if the cached data exists or not.

1
  • I never used TTL before. There is no another simple way? In my after plugin, is there a way to know if login or logout event was dispatched in the current request? – jefferson Mar 23 at 22:13
0

Can you inject the customer session (Magento\Customer\Model\Session) into your after plugin, and call ->isLoggedIn() on it after the function completes?

1
  • Of course! But I need to know if there was a toggle between login/logout. I need to program a condition to return some piece of HTML as answer but only if the customer successfully login or logout from current request. I don't want to return HTML unnecessarily – jefferson Mar 23 at 20:55
0

In your controller create a file name Index and set namespace as per your module

Here in this code, If customer logged in it returns a message and if customer logged out it redirect to customer login page

<?php
namespace Vendor\Module\Controller;

use Magento\Framework\App\Action\Context;
use Magento\Customer\Model\Session;
use Magento\Framework\Controller\Result\RedirectFactory;

class Index extends \Magento\Framework\App\Action\Action
{   
    /**
     * @var Magento\Customer\Model\Session $customerSession
     */
    protected $customerSession;

    /**
     * @var Magento\Framework\Controller\Result\RedirectFactory $resultRedirect
     */
    protected $resultRedirect;


    /**
     * @param Context $context
     * @param RedirectFactory $resultRedirect
     * @param Magento\Customer\Model\Session $customerSession
     */
    public function __construct(
        Context $context, 
        RedirectFactory $resultRedirect,
        \Magento\Customer\Model\Session $customerSession,
        array $data = []
    )
    {
        parent::__construct($context);
        $this->resultRedirect = $resultRedirect;
        $this->customerSession = $customerSession;
    }

    /**
     * @return RedirectFactory $resultRedirect
     */
    public function execute()
    {
        //If customer LoggedIn
        if($this->customerSession->isLoggedIn()){
            //You can add your own logic here
            return "Customer is logged in";
        }else{ 
            //If customer not LoggedIn redirect to Login page
            $resultRedirect = $this->resultRedirect->create();
            $resultRedirect->setPath('customer/account/login');
            return $resultRedirect;
        }
    }
}
1
  • Thanks a lot for your efforts, but its not what I'm looking for, I updated the question take a look... – jefferson Mar 25 at 5:01

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.