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.

Mohit Patel
  • 3,778
  • 4
  • 22
  • 52
Sabareesh
  • 358
  • 5
  • 28

3 Answers3

1
<script type="text/javascript">
    define(['Magento_Customer/js/model/customer'], function (customer) {
        if (customer.isLoggedIn()) {
            // Do your Logic.
        }
    });
</script>
Kishor Thummar
  • 3,000
  • 1
  • 9
  • 18
  • 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 '20 at 05:28
  • This is the Error i caught bro Mismatched anonymous define() module: function ($, customer) – Sabareesh Feb 27 '20 at 05:31
  • Please give any Solution for this bro. The error i caught is shown in the console. – Sabareesh Feb 27 '20 at 05:33
  • I have updated my answer, Check and let me know if the issue still there. – Kishor Thummar Feb 27 '20 at 05: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"); } } http://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 '20 at 05:40
  • Are you adding JS using requirejs or direct in PHTML? – Kishor Thummar Feb 27 '20 at 05:42
  • I had added in the Js file there is no requireJs in that file My file : app\code\Vendor\My Module\view\frontend\web\js\login.js – Sabareesh Feb 27 '20 at 05:44
  • And how you called this JS file for your module? – Kishor Thummar Feb 27 '20 at 05:45
  • I had added the in the this module layout file using – Sabareesh Feb 27 '20 at 05:47
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 '20 at 06: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;
        };
    }
);
Himanshu
  • 978
  • 8
  • 12