0

I have a webserver (Nodejs+express+ passport) that uses passport for local authentication. Does anyone know if there is some kind of callback occurring when user closes the browser tab? I would like to be able to detect this because I am trying to save logout time stamps for those users that do not explicitly logout (i.e. by clicking log out).

Thank you!

372
  • 237
  • 2
  • 10

2 Answers2

0

I would have asked you to check the questions on SO first, but I think is no way to do it the way you are trying. Well, check these links: how to run code on window close or refresh and how to detect it was close

Basically you can't. This is one of the reasons why many applications show 'last login time' and not logout time to users, see if that works for you.

Regarding saving the logout time of a user: I would say do not store the value at all. The sessions should be allowed to expire based on configuration: users can be logged out even by staying inactive a long time. A logout time for such users may calculated later, based on their last activity time, (which you can store on each page load/call to your server, when you refresh the user's session timer) and the session timeout value. Now this can be calculated when the user logs in the next time, or using a background process.

Community
  • 1
  • 1
NikhilWanpal
  • 2,960
  • 3
  • 23
  • 40
0
  • You can store the lastActivityDate of the user, and assume that a user is considered offline after xx minutes.

  • You can try to execute a ajax call on window unload, but if the user has two open pages, and he closed one, the data will be false. It is also not certain that the request reaches the server.

  • You can set a very short expiration time on your session, and while a page is open, do a ajax regular ping to keep the session open.

Ndrou
  • 169
  • 1
  • 5