We will include a js file in theme :
Supposing that your js file is: myfile.js
app/design/frontend/{Vendor}/{theme}/requirejs-config.js
var config = {
map: {
'*': {
biboo: 'js/myfile'
}
}
};
app/design/frontend/{Vendor}/{theme}/web/js/myfile.js
define(['jquery', 'Magento_Customer/js/model/customer'], function($, customer){
"use strict";
return function ifCustomerIsLoggedIn()
{
if (customer.isLoggedIn()) {
console.info('Yes, customer is logged in');
//your logic goes here
} else {
console.info('No, customer is not logged in');
//your logic goes here
}
}
});
app/design/frontend/{Vendor}/{theme}/Magento_Theme/templates/{yourfile}.phtml
<script>
require(['jquery', 'biboo'], function($, ifCustomerIsLoggedIn) {
ifCustomerIsLoggedIn();
});
</script>
clean the cache
clean var/view_preprocessed content
clean pub/static content
deploy the static content = php bin/magento setup:static-content:deploy -f
Output :