You can check the customer is logged in or using knockout javascript in Magento 2 by using the Magento_Customer/js/model/customer class.
customer.js file used to check customer is logged in or not.
Customer Js file contains the method isLoggedIn() to detect customer login status.
If a customer is logged in, return true otherwise return false. Create a JS file and check using the below code snippet.
/**
* @author Rakesh Jesadiya
*/
define([
'uiComponent',
'Magento_Customer/js/model/customer'
], function (
Component,
customer
) {
'use strict';
return Component.extend({
defaults: {
template: 'Rbj_MyModule/custom-file'
},
/**
* Check if customer is logged in
*
* @return {boolean}
*/
isLoggedIn: function () {
return customer.isLoggedIn();
}
});
});
I have called template custom-file.html using js. check using a template(.html) from custom-file.html by below way,
<!-- ko if: isLoggedIn() -->
<div>Customer is logged in</div>
<!-- /ko -->
Output: Boolean

works just for checkout