1

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?

G_S
  • 7,068
  • 2
  • 21
  • 51
  • I think this is best handled on the back end triggered by the user logging out/session timing out. If you must do it on the client side, the only solutions I've seen are to add listeners to any events that can trigger a reload. Those events set a flag that is checked in the unload event to determine if the navigation is reloading the page (form submit, 'F5' key, etc.). I have yet to see a solution that can account for clicking on the refresh button. – webprojohn Sep 24 '18 at 04:01
  • For user logging out, logout button handles it... Session timing out? – G_S Sep 24 '18 at 08:59
  • Keeping the topic on the original question, here is the best I've found on the subject after spending a lot of time researching and hacking at the issue: https://stackoverflow.com/a/14893469/3105371 – webprojohn Sep 24 '18 at 13:55

0 Answers0