1

I want to log in as customer frontend side.

I have created a customer listing page. But when I click on customer login button customer login without login details.

My Magento v2.0.17

  • Can you share your screenshot? – Sukumar Gorai Oct 24 '18 at 13:13
  • In which page you want to add code? – Rohan Hapani Oct 24 '18 at 13:13
  • I have created a custom block. Please check screenshot ibb.co/ek411V – Masud Shaikh Oct 24 '18 at 13:17
  • Where is a login button? – Sukumar Gorai Oct 24 '18 at 13:21
  • I have not added button now. But I will add the login button to every customer. – Masud Shaikh Oct 24 '18 at 13:23
2

Create a module with name STech_Login by following steps:

Step 1: Create registration.php under

app/code/STech/Login/registration.php

with below content:

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'STech_Login',
    __DIR__
);

Step 2: Create module.xml under

app/code/STech/Login/etc/module.xml

with below content:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="STech_Login" setup_version="0.0.1">
        <sequence>
            <module name="Magento_Customer"/>
        </sequence>
    </module>
</config>

Step 3: Create routes.xml under

app/code/STech/Login/etc/frontend/routes.xml

with below content:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/App/etc/routes.xsd">
    <router id="standard">
        <route id="login" frontName="login">
            <module name="STech_Login" />
        </route>
    </router>
</config>

Step 4: Create Lists.php under

app/code/STech/Login/Block/Customer/Lists.php

with below content:

<?php
namespace STech\Login\Block\Customer;

class Lists extends \Magento\Framework\View\Element\Template {

    protected $_customerFactory;

    public function __construct(
        \Magento\Catalog\Block\Product\Context $context,
        \Magento\Customer\Model\ResourceModel\Customer\CollectionFactory $customerFactory,
        array $data = []
    ){
        $this->_customerFactory = $customerFactory;
        parent::__construct($context, $data);
    }

    protected function _prepareLayout()
    {
        return parent::_prepareLayout();
    }

    public function getCustomers(){
        return $this->_customerFactory->create();
    }
}

Step 5: Create Index.php under

app/code/STech/Login/Controller/Index/Index.php

with below content:

<?php
namespace STech\Login\Controller\Index;

class Index extends \Magento\Framework\App\Action\Action
{
    public function execute()
    {
        $this->_view->loadLayout();
        $this->_view->getLayout()->initMessages();
        $this->_view->renderLayout();
    }
}

Step 6: Create Login.php under

app/code/STech/Login/Controller/Index/Login.php

with below content:

<?php
namespace STech\Login\Controller\Index;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\ResponseInterface;

class Login extends Action
{
    protected $_customer;
    protected $_customerSession;

    public function __construct(
        Context $context,
        \Magento\Customer\Model\Customer $customer,
        \Magento\Customer\Model\Session $customerSession
    )
    {
        $this->_customer = $customer;
        $this->_customerSession = $customerSession;
        parent::__construct($context);
    }

    public function execute()
    {
        $id = $this->getRequest()->getParam('id');
        $customer = $this->_customer->load($id); 
        $this->_customerSession->setCustomerAsLoggedIn($customer);
        if($customer->getGroupId() == YOUR_CONDITION){
            $this->_customerSession->setCustomerGroupData('ABC');
        }
        $this->_redirect('customer/account/');
    }

}

Step 7: Create login_index_index.xml under

app/code/STech/Login/view/frontend/layout/login_index_index.xml

with below content:

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
    <head>
        <title>Customer List</title>
    </head> 
    <body>
        <referenceContainer name="content">
            <block class="STech\Login\Block\Customer\Lists" name="customer_list" template="STech_Login::customers.phtml"/>
        </referenceContainer>
    </body>
</page>

Step 8: Create customers.phtml under

app/code/STech/Login/view/frontend/templates/customers.phtml

with below content:

<?php $customers = $block->getCustomers(); ?>
<ul>
<?php foreach($customers as $customer): ?>
    <li>Customer Name: <?php echo $customer->getFirstname().' '.$customer->getLastname(); ?> --- <a href="<?php echo $block->getUrl('login/index/login', ['id' => $customer->getId()]) ?>" target="_blank">LogIn</a></li>
<?php endforeach; ?>
</ul>

Run setup upgrade, di compile and static content deploy.

And test url with http://example.com/login

  • I will check and update you soon. Thanks! – Masud Shaikh Oct 24 '18 at 15:19
  • The above one will work – Avesh Naik Oct 24 '18 at 15:48
  • Hi, It is working fine. Thank you so much. – Masud Shaikh Oct 25 '18 at 6:37
  • Tick the green tick mark near my answer if it's working for you so that it will help other developers in future. – Sukumar Gorai Oct 25 '18 at 6:39
  • Hi, I have one question. When I click on the login button then how to send custom variable session after login? – Masud Shaikh Oct 25 '18 at 6:41
1

I think you are looking something like this.

https://github.com/kiwicommerce/magento2-login-as-customer

  • It is helpful for the backend side. I need the frontend. Thanks. – Masud Shaikh Oct 25 '18 at 6:44
  • you can customize the code. – Avesh Naik Oct 25 '18 at 9:07
  • Can you upvote my ans. – Avesh Naik Oct 25 '18 at 9:15
1

Method 1 :

First you need to inject \Magento\Customer\Model\Customer and \Magento\Customer\Model\Session classes in your constructor

protected $_customer;
protected $_customerSession;

public function _construct(
    ...
    \Magento\Customer\Model\Customer $customer,
    \Magento\Customer\Model\Session $customerSession
) {
    ...
    $this->_customer = $customer;
    $this->_customerSession = $customerSession;
    ...
}

Then,  add this code in your function :
$email = "test@m2s.com";
$customer = $this->_customer->loadByEmail($email); 
$this->_customerSession->setCustomerAsLoggedIn($customer);

Method 2 :

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerID = 2;
$customer = $objectManager->create('Magento\Customer\Model\Customer')->load($customerID);
$customerSession = $objectManager->create('Magento\Customer\Model\Session');
$customerSession->setCustomerAsLoggedIn($customer);

For better coding standard, Don't use object manager directly.

0

May this help

  • [yoursite_url]/customer/account/logout
  • [yoursite_url]/customer/account/login/

change [yoursite_url] with your magento installation root.

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.