0

I'm making a Facebook connect module. It add a button "facebook connect" in the logged in form and when the user log with it, it send the form with a token added in the fields.

To do that i create a plugin around the LoginPost execute and logged in my customer. It works well but after logged in the customer it doesn't redirect him to the account page but to the home page.

Here is my aroundExecute function :

 /**
     * Around the Login Post exetution check if user is connecting trough facebook
     * Create or logged in the user trough facebook
     *
     * @param \Magento\Customer\Controller\Account\LoginPost $subject Subject class
     * @param \Closure                                       $proceed Subject class function execute
     * 
     * @return mixed 
     */
    public function aroundExecute(\Magento\Customer\Controller\Account\LoginPost $subject, \Closure $proceed)
    {
        /*
         * Before execute (must follow theses conditions)
         * - The customer isn't already logged in
         * - The form token is valid
         * - The post request datas contain a data named fb_auth_token
         */
        if (!$this->session->isLoggedIn() && $this->formKeyValidator->validate($this->request) && $token = $this->request->getPost('fb_auth_token')) {
            if (!empty($token)) {
                // Get customer datas from facebook
                $userParams = $this->getDataFromFB($token);

                // If there is data provided and then an id
                if (isset($userParams->id)) {
                    // Try to load the customer by email
                    $customer = $this->customerFactory->create()
                        ->setWebsiteId($this->storeManager->getStore()->getWebsiteId())
                        ->loadByEmail($userParams->email);

                    /*
                     * Process with the loaded customer entity
                     * Check if the loaded customer exists
                     */
                    if ($customer->getId()) { // Exists
                        /*
                         * Check if the customer has fb_uid data
                         * The customer will not have a fb_uid if it register 
                         * trought native magento registration system
                         */
                        if ($customer->getData('fb_uid') == $userParams->id) { // Customer has right fb_uid
                            // Set the client logged in as the customer
                            return $this->loggedInCustomer($customer);
                        } else { // Customer does not have a fb_uid
                            // Set the customer fb_uid and save
                            $customer->setData('fb_uid', $userParams->id);
                            $customer->save();
                            // Set the client logged in as the customer
                            return $this->loggedInCustomer($customer);
                        }
                    } else if (!$this->existFbId($userParams->id)) { // Does not exists
                        // Set the customer model datas from facebook datas
                        $customer->setWebsiteId($this->storeManager->getStore()->getWebsiteId());
                        $customer->setEmail($userParams->email);
                        $customer->setFirstname($userParams->first_name);
                        $customer->setLastname($userParams->last_name);
                        $customer->setPassword($this->random->getRandomString(10));
                        $customer->setData('fb_uid', $userParams->id);
                        // Save the customer
                        $customer->save();
                        // Send the welcome email
                        $customer->sendNewAccountEmail();
                        // Set the client logged in as the customer
                        return $this->loggedInCustomer($customer);
                    }
                }
            }
        }

        // Hook to the higher execute level
        $resultProceed = $proceed();

        // Hook to the higher postExecute level
        return $resultProceed;
    }

Here is the loggedInCustomer function :

/**
     * Set logged in as a customer
     *
     * @param \Magento\Customer\Model\Customer $customer customer to logged in
     * 
     * @return ResultRedirect
     */
    protected function loggedInCustomer($customer)
    {
        // Set the customer as logged in
        $this->session->setCustomerAsLoggedIn($customer);

        // Rebuild the mage cache session id cookie
        if ($this->cookieManager->getCookie('mage-cache-sessid')) {
            $metadata = $this->cookieMetadataFactory
                ->createCookieMetadata()
                ->setPath('/');
            $this->cookieManager->deleteCookie('mage-cache-sessid', $metadata);
        }

        // Redirect to the right url
        $redirectUrl = $this->accountRedirect->getRedirectCookie();
        if (!$this->scopeConfig->getValue('customer/startup/redirect_dashboard') && $redirectUrl) {
            $this->accountRedirect->clearRedirectCookie();
            $resultRedirect = $this->resultRedirectFactory->create();
            $resultRedirect->setUrl($this->redirect->success($redirectUrl));
            return $resultRedirect;
        }

        return $this->accountRedirect->getRedirect();
    }

I followed the native logged in system i search trought the magento core files to make this. I don't understand what's wrong because it's the same in the LoginPost controller...

1
  • I hope, you have checked setting as i suggested.. Commented Feb 5, 2019 at 11:51

1 Answer 1

0

Do this admin setting

Stores => Configuration > Customers > Customer Configuration > Login Options > Redirect Customer to Account Dashboard after Logging in > Yes

clear cache and check..

1
  • I checked and no the setting was not set. I set it and it works now. Thanks for yout helping. I didn't checked it because i tough it was already set as true. When i loggin by using credentials and clicking in sign in button, i'm redirected to the account page whereas the settings was to false. How could you explain this ?
    – Kpone
    Commented Feb 5, 2019 at 11:59

Your Answer

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

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