We are working on a product where licencing is involved. It is build on reactJS, redux, asp.net core and also uses Signalr for broadcasting messages to client.
Licence is counted as below.
- Predefined value for max number of browsers the product can login from.
- Whenever a user logins from a browser, a licence will be consumed.
- Whenever a user logouts from browser, licence will be released.
Whenever application is opened from the same browser where user already logged in, it navigates to default screen. (local storage is used to check whether user is already logged in)
Everything is good until the user refreshes or closes the tab directly. When there is only one tab opened and if its closing, we have planned to logout the user and reduce the licence count.
In componentWillmount, we have used below logic ( 'beforeunload' window event) to reduce licence count.
window.addEventListener("beforeunload", (event) => {
this.props.deleteThisTabFromLocalStorage();
if(tabsCountFromLocalStorage == 0){
this.props.ReleaseLicence();
this.props.Logout(); // This method contains redirection to login route. and deleting access tokens from localstorage.
}
});
Expected that 'beforeunload' will be called only for browser close. But when the tab is refreshed, even in this case 'beforeunload' event is getting called and application is getting loggedout.
Can someone suggest on how to differentiate browser close and refresh events?