0

I copy the js code from How to check that customer is logged in on frontend in Magento 2.1? I have defined the js code on my javascript, but I do not how to get the return value. How to get the return value?

/**
 * ****/js/check_login.js
 */
define(
    ['jquery', 'Magento_Customer/js/customer-data'],
    function ($, customerData) {
        'use strict';

        var getCustomerInfo = function () {
            var customer = customerData.get('customer');

            return customer();
        };

        var isLoggedIn = function (customerInfo) {
            customerInfo = customerInfo || getCustomerInfo();

            return customerInfo && customerInfo.firstname;
        };

        return function () {
            var deferred = $.Deferred();
            var customerInfo = getCustomerInfo();

            if (customerInfo && customerInfo.data_id) {
                deferred.resolve(isLoggedIn(customerInfo));
            } else {
                customerData.reload(['customer'], false)
                    .done(function () {
                        deferred.resolve(isLoggedIn());
                    })
                    .fail(function () {
                        deferred.reject();
                    });
            }

            return deferred;
        };
    }
);

define([
    'uiComponent',
    'jquery',
    'Magento_Ui/js/modal/modal',
    'Magento_Customer/js/customer-data',
    'Vendor_Module/js/check_login'
], function (Component, $, modal, storage, checklogin) {
    'use strict';

    console.log(checklogin());

2 Answers 2

1

I managed to get what I needed

define([
    'uiComponent',
    'jquery',
    'Magento_Ui/js/modal/modal',
    'Magento_Customer/js/customer-data',
    'Vendor_Module/js/check_login'
], function (Component, $, modal, storage, checklogin) {
    'use strict';

    var getLogin = checklogin();

    // firstname is the return value from check_login.js
    getLogin.then(function(firstname){
        // you code here
    });

});
0
<div class="text-account text-hidden" data-bind="scope: 'customer'">
    <!-- ko if: customer().firstname  -->
    <span class="logged-in"
         data-bind="text: new String('<?= $block->escapeHtml(__('Hello!, %1', '%1')) ?>').replace('%1', customer().firstname)">
    </span>
    <!-- /ko -->
    <!-- ko ifnot: customer().firstname  -->
        <?php echo __('Hello! Sign in'); ?>
    <!-- /ko -->
    <script type="text/x-magento-init">
    {
        "*": {
            "Magento_Ui/js/core/app": {
                "components": {
                    "customer": {
                        "component": "Magento_Customer/js/view/customer"
                    }
                }
            }
        }
    }
    </script>
</div>

i used to check login this way

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.