Easy way to check whether customer logged in or not via customer Js component in phtml file.
<div data-bind="scope: 'customer'">
<!-- ko if: customer().fullname -->
Logged In customer
<!-- /ko -->
<!-- ko ifnot: customer().fullname -->
Notlogged In customer
<!-- /ko -->
</div>
<script type="text/x-magento-init">
{
"*": {
"Magento_Ui/js/core/app": {
"components": {
"customer": {
"component": "Magento_Customer/js/view/customer"
}
}
}
}
}
</script>
If you want to use it in the js file
define([
'uiComponent',
'Magento_Customer/js/model/customer'
], function (
Component,
customer
) {
'use strict';
return Component.extend({
/**
* Check if customer is logged in
*
* @return {boolean}
*/
isLoggedIn: function () {
return customer.isLoggedIn();
}
});
});
You can call in your related HTML template.
<!-- ko if: isLoggedIn() -->
<div>Customer is logged in</div>
<!-- /ko -->

