1

I created a new CMS site newsletter-abo and placed a text on it which shows this:

Please login to access this site.

The word "login" points to /customer/account/login.

If the customer clicks on this link, then he gets redirected to the login site.

We need the customer to get redirected back to the newsletter-abo site after the login.

I know you can do this by adding a encrypted text to the URL, but I don't know how I can do it.

Please advice.

3 Answers 3

1

You can use below code for this:

$url  = $this->getUrl('*/*/*', ['_current' => true, '_use_rewrite' => true]);
$login_url = $block->getUrl('customer/account/login', array('referer' => base64_encode($url)));
1

You could create your custom controller class to which you point from your CMS page. Inject Magento\Framework\UrlInterface in the controller constructor and add the following piece of code in the execute method:

public function execute()
{
    $loginRoute = $this->urlInterface
        ->getUrl('customer/account/login',
        ['referer' => base64_encode($this->_redirect->getRefererUrl())]
    );
    $redirect = $this->resultRedirectFactory->create();
    $redirect->setUrl($loginRoute);

    return $redirect;
}
0

You need to call observer after successfull customer_login.

Step 1:- Please create events.xml file under path PackageName/Module/etc/frontend/

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="customer_login">
        <observer name="customer_login_success" instance="PackageName\Module\Observer\CustomerLogin" />
    </event>
</config>

Step 2: Create CustomerLogin.php file under path PackageName/Module/Observer/

<?php
namespace PackageName\Module\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;

class CustomerLogin implements ObserverInterface
{
     /**
     * @var \Magento\Framework\App\ResponseFactory
     */
    protected $responseFactory;

    public function __construct(
        \Magento\Framework\App\ResponseFactory $responseFactory
    ) {
        $this->responseFactory = $responseFactory;
    }

    public function execute(Observer $observer)
    {
        $redirectUrl = 'https://domain.com/newsletter-abo'; // add your redirect url
        $resultRedirect = $this->responseFactory->create();
        $resultRedirect->setRedirect($redirectUrl)->sendResponse('200');
        exit();
    }
}

With the above you can redirect as per you want after login.

1
  • But this will always redirect to newsletter-abo. I only need to redirect after a link was clicked.
    – Black
    Jun 8, 2021 at 9:27

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.