0

I have added a login link in Magento top links by using deafult.xml and set the URL as default Magento sign-in URL

default.xml

<block class="Learning\Module\Block\Link1" name="custom-header-linkyy">

                <arguments>
                <argument name="label" xsi:type="string" translate="true">Login</argument>
                <argument name="path" xsi:type="string" translate="true">customer/account/login/</argument>
                </arguments>

          </block> 

     </referenceBlock>

link1.php

<?php
namespace Learning\Module\Block;

class Link1 extends \Magento\Framework\View\Element\Html\Link
{
/**
* Render block HTML.
*
* @return string
*/
protected function _toHtml()
    {
     if (false != $this->getTemplate()) 
     {
     return parent::_toHtml();
     }
     return '<li ><a ' . $this->getLinkAttributes() . ' >' . $this->escapeHtml($this->getLabel()) . '</a></li>';
    }
}

now what I want is when a customer clicks and logged in successfully that button should be changed to logout is there any solution for this thanks in advance

Pramod
  • 1,464
  • 1
  • 11
  • 38

5 Answers5

1

In Your Button You Can Check Condition :-

$this->isLoggedIn() ? __('Log Out') : __('Log In')
Ronak Rathod
  • 6,322
  • 17
  • 42
0

Please check this answer which will show you the best way to check for if session is logged in.

Creating a helper to use in your templates:

<?php
namespace MyVendor\MyModule\Helper;

use Magento\Framework\App\Helper\AbstractHelper;

/**
 * Created by Carl Owens (carl@partfire.co.uk)
 * Company: PartFire Ltd (www.partfire.co.uk)
 **/
class Data extends AbstractHelper
{
    /**
     * @var \Magento\Framework\App\Http\Context
     */
    private $httpContext;

    public function __construct(
        \Magento\Framework\App\Helper\Context $context,
        \Magento\Framework\App\Http\Context $httpContext
    ) {
        parent::__construct($context);
        $this->httpContext = $httpContext;
    }

    public function isLoggedIn()
    {
        $isLoggedIn = $this->httpContext->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH);
        return $isLoggedIn;
    }
}

Then I use the helper in your templates like so:

<?php
$helper = $this->helper('MyVendor\MyModule\Helper\Data');

if ($helper->isLoggedIn()) {
    //show something
}

Taken from : https://magento.stackexchange.com/a/141599/7863

paj
  • 5,785
  • 5
  • 21
  • 43
0

You can add Login, Logout button Using Object Manager

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManager->get('Magento\Customer\Model\Session');
if($customerSession->isLoggedIn()) {
   // customer login action
}

In Controller

$this->_objectManager->get('Magento\Customer\Model\Session');
if($customerSession->isLoggedIn()) {
   // customer login action
}

Using Helper

<?php $_loggedin = $this->helper('Magento\Checkout\Helper\Cart')->getCart()->getCustomerSession()->isLoggedIn(); ?>

<?php if( $_loggedin ) : ?>

     <div><!-- add your code --></div>

<?php endif; ?>
Vrajesh Patel
  • 1,964
  • 2
  • 12
  • 28
0

You Can try Below code

Vendor/Module/view/frontend/layout/default.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="header.links">
            <block class="Vendor\Module\Block\Index\Link" name="custom_link" before="authorization-link" template="Vendor_Module::index/link.phtml"/>
        </referenceBlock>
    </body>
</page>

Vendor/Module/Block/Index/Link.php

<?php
namespace Vendor\Module\Block\Index;

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


    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Customer\Model\SessionFactory $customerSession,
        array $data = []
    ) {
        $this->storeManager = $context->getStoreManager();
        $this->customerSession = $customerSession;
        parent::__construct($context, $data);
    }

    public function isCustomerLoggedIn()
    {
        if ($this->customerSession->create()->isLoggedIn()) {
            return true;
        }
        return false;
    }
}

Vendor/Module/view/frontend/templates/index/link.phtml

<?php

if ($block->isCustomerLoggedIn()) {
    ?>
    <li>
        <a href="<?php echo $block->getBaseUrl() . 'customer/account/login';?>" id="login_form">
            <?php echo __("Log in") ?>
        </a>
    </li>
<?php 
}else{ ?>
    <li>
        <a href="<?php echo $block->getBaseUrl() . 'customer/account/logout';?>" id="logout_form">
            <?php echo __("Log Out") ?>
        </a>
    </li>
<?php }
?> 
Mohit Rane
  • 1,955
  • 1
  • 13
  • 47
0

Please take a look at least how it is implemented in magento 2 currently.

Just go to: vendor/magento/module-customer/view/frontend/templates/account/link/authorization.phtml

and here we got

if($block->isLoggedIn())

Now lets go to the responsible class/block.

vendor/magento/module-customer/Block/Account/AuthorizationLink.php

public function isLoggedIn()

Add this block to your constructor and run isLoggedIn function where you need it or inject the httpContext and apply the logic.

Gosu Przmak
  • 275
  • 1
  • 14