Logout User When Window Closes


#1

How can I logout the user when all windows opened with the app closes? The following code only logouts the user when one of the browser window is closed. If the user has multiple windows with the app opened, it will logout all windows using the same user.

onbeforeunload = function () {
    Meteor.logout();
}

#2

You could use a package like tmeasday:presence to detect how many sessions the user has, and when it is down to one, you can activate the onbeforeunload


#3

@serkandurusoy Thanks for your response. I am using the mizzao:user-status package. How can I detect how many sessions the user has? Should I count how much documents there are in the ipAddr field in the UserStatus.connections for a user?


#4

I’m not an avid user of mizzao:user-status but the documentation suggests something like

if (Meteor.users.find({_id: Meteor.userId(),  "status.online": true }).count === 1) {
  /* this should probably be the user's last active session */
}

#5

Thanks. I’ve decided just to warn users when they are unloading the page that their session will be continued if they do not logout themselves.


#6

I also had the same problem and found a solution that really well fits what was common in the pre-meteor-era