0

I want find the whether the customer is logged in or not using Js. I got some links but it dosen't works and i not good at knockout js. Please give solution with brief explanation

Referred Link :

How to check that customer is logged in on frontend in Magento 2.1?

Thanks.

1
<script type="text/javascript">
    define(['Magento_Customer/js/model/customer'], function (customer) {
        if (customer.isLoggedIn()) {
            // Do your Logic.
        }
    });
</script>
  • Iam getting this kind of error Mismatched anonymous define() module: function ($, customer) My code define(['jquery', 'Magento_Customer/js/model/customer'], function ($, customer) { console.log("Second Way"); if (customer.isLoggedIn()) { console.log("Second Way"); alert("Entered"); } }); – Sabareesh Feb 27 at 5:28
  • This is the Error i caught bro Mismatched anonymous define() module: function ($, customer) – Sabareesh Feb 27 at 5:31
  • Please give any Solution for this bro. The error i caught is shown in the console. – Sabareesh Feb 27 at 5:33
  • I have updated my answer, Check and let me know if the issue still there. – Kishor Thummar Feb 27 at 5:36
  • Yes iam getting the same thing bro Uncaught Error: Mismatched anonymous define() module: function (customer) { console.log("Second Way"); if (customer.isLoggedIn()) { console.log("Second Way"); alert("Entered"); } } requirejs.org/docs/errors.html#mismatch at makeError (require.js:166) at intakeDefines (require.js:1221) at Object.localRequire [as require] (require.js:1402) at window.require (mixins.js:245) at index:49 – Sabareesh Feb 27 at 5:40
0
 require(['Magento_Customer/js/model/customer'], function (customer) {
        if (customer.isLoggedIn()) {
            console.log('Logged In');
        } else {
            console.log('Not Logged In');
        }
    });
  • Thank You it didn't gives any error but it shows only the else part when the customer is logged in also. @Shailesh Katarmal – Sabareesh Feb 27 at 6:34
-1
**
 * This file will check that customer is logged in
 */
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 () {
                        console.log(isLoggedIn());
                    })
                    .fail(function () {
                        deferred.reject();
                    });
            }

            return deferred;
        };
    }
);

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.