bitExpert / magento2-force-login

Force Customer Login Module for Magento 2
https://marketplace.magento.com/bitexpert-magento2-force-customer-login.html
Apache License 2.0
166 stars 73 forks source link

multistore / subfolder / redirect to root #181

Closed nelero closed 8 months ago

nelero commented 4 years ago

Hi, if you have multistore configuration, with a website configurated in subfolder, multiples problems occured :

if error occured on login, the visitor is redirected to root level (website) and not the website he was trying to log in.

Configuration default website : https://mywebsite.com/ Force login not enabled

Configuration first website : https://mywebsite.com/ force login not enabled

Configuration second website : https://anothersite.com/**specific/ force login ENABLED**

configuration third website : https://anothersite.com/ force login not enabled

trying to access homepage for https://anothersite.com/**specific**/ redirect to https://anothersite.com/specific/customer/account/login even with default entries.

Making error in logging in to https://anothersite.com/specific/customer/account/login redirects visitor to https://anothersite.com/ instead of https://anothersite.com/**specific**/

Preconditions

Magento Version : 2.3.4

Force Login Module Version : Latest

Even if we add entries for this specific store, we had to precise everytime the subfolder.

shochdoerfer commented 4 years ago

@nelero thanks for your first bug report. Mind providing a PR to fix this issue?

maurixxxx commented 3 years ago

Hi i got the same problem, did you find a solution??

der-workfloh commented 3 years ago

@nelero Latest version was latest "stable" version, or dev-master? @nelero @maurixxxx we added some fixes to the master, do you mind testing your issue, if everything works fine now or report any (new) issue occuring?

maurixxxx commented 3 years ago

I'll try it

maurixxxx commented 3 years ago

I did the upgrade to the 4.1.0 version but it doesn't fix the issue. After login customer is redirect to the main domain.

maurixxxx commented 3 years ago

and if i disable the module everything goes right.

nenriquez commented 3 years ago

In my opinion the problem is the AfterLoginPlugin, a part of code is:

        $targetUrl = $this->session->getAfterLoginReferer();
        if (empty($targetUrl)) {
            $targetUrl = $this->defaultTargetUrl;
        }

If Redirect Customer to Account Dashboard after Logging in is NO then targetUrl allway is empty. For that defaultTargetUrl is used as final url. As you can see in di.xml file defaultTargetUrl is / instead of /spesific/.

    <type name="BitExpert\ForceCustomerLogin\Plugin\AfterLoginPlugin">
        <arguments>
            <argument name="defaultTargetUrl" xsi:type="string">/</argument>
        </arguments>
    </type>

Maybe there is an explanation about this behaviour but it doesn't have for me.

I've write a custom module that have a plugin of bitExpert's AfterLoginPlugin. This plugin dynamically decide which is the final url:

    public function afterAfterExecute(\BitExpert\ForceCustomerLogin\Plugin\AfterLoginPlugin $overrided, $resultRedirect)
    {
        $targetUrl = $this->session->getAfterLoginReferer();
        if (empty($targetUrl) && $this->session->isSessionExists()) {
            $resultRedirect->setUrl($url);
        }
        return $resultRedirect;
    }

    private function getBaseUrl()
    {
        $secure = $this->getForceSecureRedirectOption();
        $secure = ($secure === true) ? true : null;
        return $this->storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB, $secure);
    }

    private function getForceSecureRedirectOption()
    {
        return (bool) $this->scopeConfig->getValue(
            LoginCheckInterface::MODULE_CONFIG_FORCE_SECURE_REDIRECT,
            ScopeInterface::SCOPE_STORE
        );
    }

Anyway bitExpert team, thanks for the great plug in.

shochdoerfer commented 10 months ago

@rikwillems if you are up for another task, this one has been open for way too long now :)

rikwillems commented 8 months ago

@nelero working with subdirectories can be a great pain. I think your base url is not properly resolved since that is used in the Magento redirect.

Since some time I have used an additional php file to determine store code and that works very good. With that solution I cannot reproduce the issue you describe as the base url is properly resolved.

Append your composer.json with an additional autoload file


    "autoload": {
        "files": [
            "app/etc/NonComposerComponentRegistration.php",
            "app/etc/stores.php"
        ],
    },

Do some magic to determine store code in app/etc/stores.php

<?php

use \Magento\Store\Model\StoreManager;

if (!isset($_SERVER['REQUEST_URI'])) {
    return;
}

$hostName = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : null;
$uriParts = explode('/', trim($_SERVER['REQUEST_URI'], '/'));
$code = '';
$runCode = 'default';
$runType = 'store';

$subdirectories = [
    'subdir',
];

$domainRegexes = [
    '(?P<code>(second|third)).ce24.test',
];

foreach ($domainRegexes as $regex) {
    if (preg_match('/'.$regex.'$/i', $hostName, $matches)) {
        $code = $matches['code'];
        break;
    }
}

$runCodeMapping = [
    'second' => 'second',
    'third' => 'third',
];

$runCode = $runCodeMapping[$code] ?? $runCode;

if (in_array($uriParts[0], $subdirectories)) {
    $_SERVER['REQUEST_URI'] = preg_replace('/\/' . $uriParts[0] . '/', '', $_SERVER['REQUEST_URI'], 1);
    $runCode = $uriParts[0];
}

$_SERVER[StoreManager::PARAM_RUN_TYPE] = $runType;
$_SERVER[StoreManager::PARAM_RUN_CODE] = $runCode;
nelero commented 8 months ago

Thanks for your reply. I'm not concerned with this issue anymore, as we moved to subdomains since 3 years now.

Regards,

Le mer. 3 janv. 2024 à 13:57, Rik Willems @.***> a écrit :

@nelero https://github.com/nelero working with subdirectories can be a great pain. I think your base url is not properly resolved since that is used in the Magento redirect.

Since some time I have used an additional php file to determine store code and that works very good. With that solution I cannot reproduce the issue you describe as the base url is properly resolved.

Append your composer.json with an additional autoload file

"autoload": {
    "files": [
        "app/etc/NonComposerComponentRegistration.php",
        "app/etc/stores.php"
    ],
},

Do some magic to determine store code in app/etc/stores.php

<?php

use \Magento\Store\Model\StoreManager;

if (!isset($_SERVER['REQUEST_URI'])) { return; }

$hostName = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : null; $uriParts = explode('/', trim($_SERVER['REQUEST_URI'], '/')); $code = ''; $runCode = 'default'; $runType = 'store';

$subdirectories = [ 'subdir', ];

$domainRegexes = [ '(?P(second|third)).ce24.test', ];

foreach ($domainRegexes as $regex) { if (preg_match('/'.$regex.'$/i', $hostName, $matches)) { $code = $matches['code']; break; } }

$runCodeMapping = [ 'second' => 'second', 'third' => 'third', ];

$runCode = $runCodeMapping[$code] ?? $runCode;

if (in_array($uriParts[0], $subdirectories)) { $_SERVER['REQUEST_URI'] = preg_replace('/\/' . $uriParts[0] . '/', '', $_SERVER['REQUEST_URI'], 1); $runCode = $uriParts[0]; }

$_SERVER[StoreManager::PARAM_RUN_TYPE] = $runType; $_SERVER[StoreManager::PARAM_RUN_CODE] = $runCode;

— Reply to this email directly, view it on GitHub https://github.com/bitExpert/magento2-force-login/issues/181#issuecomment-1875332407, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAEQ6MW32OSI4QPILKMC5ITYMVISRAVCNFSM4LRLYZE2U5DIOJSWCZC7NNSXTN2JONZXKZKDN5WW2ZLOOQ5TCOBXGUZTGMRUGA3Q . You are receiving this because you were mentioned.Message ID: @.***>

rikwillems commented 8 months ago

@shochdoerfer I am closing this issue.

shochdoerfer commented 8 months ago

@rikwillems thx for posting your explanation here. Maybe someone will find it in the future :)