If you want to keep track of user logins and logouts in a Meteor application, you can utilize Deps.autorun to create a custom handler observing changes in the reactive variable Meteor.userId(). This variable returns the currently logged-in user's ID (null if no one is logged in) and their corresponding user document in the Meteor.users collection.
By monitoring the modifications of these reactive data sources, you can effectively monitor when users sign in or out of your application.
In your client/main.js file, you can set up a local variable (lastUser) to store the information of the last logged-in user. Within the Meteor.startup function, you can use Deps.autorun to create a reactive context that tracks changes in Meteor.userId() and reacts accordingly.
If a user logs out, you won't be able to access Meteor.user(), but you can still retrieve the last user's details from the lastUser variable. You can then call a server method passing the lastUser._id as an argument to make any necessary modifications to the user document upon logout.
In your server/server.js file, you can define a method called userDisconnected which takes the user's ID as an argument and retrieves the user document for further processing.
Remember to implement proper verification mechanisms to prevent malicious clients from abusing this server method by passing arbitrary user IDs for unauthorized actions.