0

I created a custom login page and everything is working fine except when login is incorrect. The page only refresh without displaying the error message.

Here're my codes

app/code/Testing/Basic/etc/di.xml

    <?xml version="1.0"?>
    <!--
    /** Grid di xml
     * @category  Webkul
     * @package   Webkul_Grid
     * @author    Webkul
     * @copyright Copyright (c) 2010-2017 Webkul Software Private Limited (https://webkul.com)
     * @license   https://store.webkul.com/license.html
     */
    -->
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
        <preference for="Testing\Basic\Api\Data\WebpageInterface" type="Testing\Basic\Model\Webpage" />
        <preference for="Testing\Basic\Api\Data\WebappNewequipment" type="Testing\Basic\Model\Equipment" />
        <preference for="Magento\Customer\Controller\Account\LoginPost" type="Testing\Basic\Controller\Account\Loginpost" />
.............

app/design/frontend/CustomTheme/customtheme/Magento_Customer/layout/customer_account_login.xml

<?xml version="1.0"?>
<!--
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <head>
        <title>Customer Login</title>
    </head>
    <body>
        <referenceContainer name="page.messages">
            <block class="Magento\Framework\View\Element\Template" name="ajax.message.placeholder" template="Magento_Theme::html/messages.phtml"/>
            <block class="Magento\Framework\View\Element\Messages" name="messages" as="messages" template="Magento_Theme::messages.phtml"/>
        </referenceContainer>
        <referenceContainer name="content">
            <!-- customer.form.login.extra -->
            <container name="customer.login.container" label="Customer Login Container" htmlTag="div" htmlClass="content">
                <block class="Magento\Customer\Block\Form\Login" name="customer_form_login" template="Magento_Customer::form/login.phtml">
                    <container name="form.additional.info" as="form_additional_info"/>
                </block>
                <block class="Magento\Customer\Block\Form\Login\Info" name="customer.new" template="Magento_Customer::newcustomer.phtml"/>
            </container>
            <block class="Magento\Cookie\Block\RequireCookie" name="require-cookie" template="Magento_Cookie::require_cookie.phtml">
                <arguments>
                    <argument name="triggers" xsi:type="array">
                        <item name="loginButton" xsi:type="string">.action.login</item>
                    </argument>
                </arguments>
            </block>
        </referenceContainer>
        <referenceContainer name="form.additional.info">
            <block class="Magento\Framework\View\Element\Template" name="form_additional_info_customer" template="Magento_Customer::additionalinfocustomer.phtml"/>
        </referenceContainer>
    </body>
</page>

app/design/frontend/CustomTheme/customtheme/Magento_Customer/template/form/login.phtml

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

/** @var \Magento\Customer\Block\Form\Login $block */
?>
<div class="block block-customer-login">
    <h1>Dealer Login</h1>
    <p>
        At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occa ecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinct.
    </p>
    <form class="form form-login"
          action="<?= $block->escapeUrl($block->getPostActionUrl()) ?>"
          method="post"
          id="login-form"
          data-mage-init='{"validation":{}}'>
        <?= $block->getBlockHtml('formkey') ?>
                        <div class="form-item">
        <div class="dealer-login-form">
            <div class="row justify-content-md-center">
                <div class="col-md-8 col-lg-6">
                    <div class="dealer-login-box">
                        <div class="form-item">
                            <label>Chassis Pool</label>
                            <div class="select-box">
                                <select name="login[zone]" id="zone" name="brand">
                                    <option value="">Select</option>
                                    <option value="1">Ford</option>
                                    <option value="2">Chevy</option>
                                    <option value="3">GMC</option>
                                </select>
                            </div>
                        </div>
                        <div class="form-item">
                            <label><?= $block->escapeHtml(__('Email Address')) ?></label>
                            <input name="login[username]" value="<?= $block->escapeHtmlAttr($block->getUsername()) ?>" <?php if ($block->isAutocompleteDisabled()) : ?> autocomplete="off"<?php endif; ?> id="email" type="email" class="input-text" title="<?= $block->escapeHtmlAttr(__('Email')) ?>" data-mage-init='{"mage/trim-input":{}}' data-validate="{required:true, 'validate-email':true}">
                        </div>
                        <div class="form-item">
                            <label class="password-label left"><?= $block->escapeHtml(__('Password')) ?></label>
                            <label class="password-label right" ><a href="<?= $block->escapeUrl($block->getForgotPasswordUrl()) ?>"><?= $block->escapeHtml(__('Forgot Your Password?')) ?></a></label>
                            <input name="login[password]" type="password" <?php if ($block->isAutocompleteDisabled()) : ?> autocomplete="off"<?php endif; ?> class="input-text" id="pass" title="<?= $block->escapeHtmlAttr(__('Password')) ?>" data-validate="{required:true}">
                        </div>
                         <?= $block->getChildHtml('form_additional_info') ?>
                        <div class="form-item form-action">
                            <input type="submit" value="<?= $block->escapeHtml(__('Login')) ?>" class="btn-medium" />
                        </div>

                    </div>  
                </div>
            </div>
        </div>
    </form>
</div>

app/code/Testing/Basic/Controller/Account/Loginpost.php

<?php

namespace Testing\Basic\Controller\Account;

use Magento\Customer\Model\Account\Redirect as AccountRedirect;
use Magento\Framework\App\Action\Context;
use Magento\Customer\Model\Session;
use Magento\Customer\Api\AccountManagementInterface;
use Magento\Customer\Model\Url as CustomerUrl;
use Magento\Framework\Exception\EmailNotConfirmedException;
use Magento\Framework\Exception\AuthenticationException;
use Magento\Framework\Data\Form\FormKey\Validator;

/**
 *  * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
 *   */
class LoginPost extends \Magento\Customer\Controller\Account\LoginPost {

    public function execute() {
        if ($this->session->isLoggedIn() || !$this->formKeyValidator->validate($this->getRequest())) {
            /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
            $resultRedirect = $this->resultRedirectFactory->create();
            $resultRedirect->setPath('*/*/');
            return $resultRedirect;
        }


        if ($this->getRequest()->isPost()) {
            $login = $this->getRequest()->getPost('login');
            if (!empty($login['username']) && !empty($login['password'])) {
                try {
                    $customer = $this->customerAccountManagement->authenticate($login['username'], $login['password']);
                    $this->session->setCustomerDataAsLoggedIn($customer);
                } catch (EmailNotConfirmedException $e) {
                    echo 'as';die;
                    $value = $this->customerUrl->getEmailConfirmationUrl($login['username']);
                    $message = __(
                        'This account is not confirmed.' .
                        ' <a href="%1">Click here</a> to resend confirmation email.', $value
                    );
                    $this->messageManager->addError($message);
                    $this->session->setUsername($login['username']);
                } catch (AuthenticationException $e) {
                    $message = __('Invalid login or password.');
                    $this->messageManager->addError($message);
                    $this->session->setUsername($login['username']);
                } catch (\Exception $e) {
                    $this->messageManager->addError(__('Invalid login or password.'));
                }

            } else {
                $this->messageManager->addError(__('A login and a password are required.'));
            }
        }

        //


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

    }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.