Feel free to share this solution with Magento Community via social media.
When a non-logged in user tries to add the product to the wishlist, the default Magento 2 generates one referer URL and redirects that user to the login page. For example, if you write customer/account after your store URL without logging in, Magento 2 automatically redirects you to the login page using the referer URL.
You can create referer URL in Magento 2 to implement the below functionalities in your store.
Never miss Magento tips, tricks, tutorials, and news.
- Restrict user to access order page directly without login.
- Direct URL access restriction without logging in.
- Restrict payments using a specific method for non-logged-in users.
Referer URL is the address of the webpage where a person clicked a link that sent them to your page. Referer is the webpage that the visitor was on before landing on your page.
Method to Create Referer URL in Magento 2
class Index extends ActionAction
public function __construct(ActionContext $context, UrlInterface $urlInterface)
$this->urlInterface = $urlInterface;
public function execute()
$url = $this->_redirect->getRefererUrl();
$login_url = $this->urlInterface
array(‘referer’ => base64_encode($url))
$resultRedirect = $this->resultRedirectFactory->create();
Follow the below solution and restrict the user to access your pages directly without login and get rid of unauthenticated users.
Something went wrong.
Thank you for subscribing.
It’s the default behaviour of the Magento 2, but what if you want the same concept in other pages and functionalities on your store also?
Get Weekly Updates
You are always welcome to mention your doubts regarding this solution in the Comments section below.
I would be glad to help you.