Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Picking up meteor.js user logout

Is there any way to pick up when a user logs out of the website? I need to do some clean up when they do so. Using the built-in meteor.js user accounts.

I'll be doing some validation using it, so I need a solution that cannot be trigger on behalf of other users on the client side - preferably something completely server side.

like image 801
Evan Knowles Avatar asked Jul 29 '13 11:07

Evan Knowles


3 Answers

You may use Deps.autorun to setup a custom handler observing Meteor.userId() reactive variable changes.

Meteor.userId() (and Meteor.user()) are reactive variables returning respectively the currently logged in userId (null if none) and the corresponding user document (record) in the Meteor.users collection.

As a consequence one can track signing in/out of a Meteor application by reacting to the modification of those reactive data sources.

client/main.js :

var lastUser=null;

Meteor.startup(function(){
    Deps.autorun(function(){
        var userId=Meteor.userId();
        if(userId){
            console.log(userId+" connected");
            // do something with Meteor.user()
        }
        else if(lastUser){
            console.log(lastUser._id+" disconnected");
            // can't use Meteor.user() anymore
            // do something with lastUser (read-only !)
            Meteor.call("userDisconnected",lastUser._id);
        }
        lastUser=Meteor.user();
    });
});

In this code sample, I'm setting up a source file local variable (lastUser) to keep track of the last user that was logged in the application. Then in Meteor.startup, I use Deps.autorun to setup a reactive context (code that will get re-executed whenever one of the reactive data sources accessed is modified). This reactive context tracks Meteor.userId() variation and reacts accordingly.

In the deconnection code, you can't use Meteor.user() but if you want to access the last user document you can use the lastUser variable. You can call a server method with the lastUser._id as argument if you want to modify the document after logging out.

server/server.js

Meteor.methods({
    userDisconnected:function(userId){
        check(userId,String);
        var user=Meteor.users.findOne(userId);
        // do something with user (read-write)
    }
});

Be aware though that malicious clients can call this server method with anyone userId, so you shouldn't do anything critical unless you setup some verification code.

like image 103
saimeunt Avatar answered Sep 20 '22 13:09

saimeunt


Use the user-status package that I've created: https://github.com/mizzao/meteor-user-status. This is completely server-side.

See the docs for usage, but you can attach an event handler to a session logout:

UserStatus.events.on "connectionLogout", (fields) ->
  console.log(fields.userId + " with connection " + fields.connectionId + " logged out")

Note that a user can be logged in from different places at once with multiple sessions. This smart package detects all of them as well as whether the user is online at all. For more information or to implement your own method, check out the code.

Currently the package doesn't distinguish between browser window closes and logouts, and treats them as the same.

like image 32
Andrew Mao Avatar answered Sep 20 '22 13:09

Andrew Mao


We had a similar, though not exact requirement. We wanted to do a bit of clean up on the client when they signed out. We did it by hijacking Meteor.logout:

if (Meteor.isClient) {
  var _logout = Meteor.logout;
  Meteor.logout = function customLogout() {
    // Do your thing here
    _logout.apply(Meteor, arguments);
  }
}
like image 40
u2622 Avatar answered Sep 20 '22 13:09

u2622