Search code examples
javascriptmeteormeteor-accounts

After Meteor.loginWithPassword old user does not go offline (meteor-user-status package)


I have a webpage where users can easily switch and login as another user. So I have this form that lets enter username/password and user is automatically signed into that new user. Here how I do it:

        'submit #loginForm': function(event) {
            event.preventDefault();
            var username = $("#usernameInput").val();
            var password = $("#passwordInput").val();
            var loginCallback = function (error) {
                if (error) {
                    FlashMessages.sendError(error.message);
                } else {
                    FlashMessages.sendSuccess("Logged in as " + username + " successfully");
                    Meteor.logoutOtherClients(function(error) {
                        if (error) {
                            FlashMessages.sendError(error.message);
                        }
                    });
                }
            };
            Meteor.loginWithPassword(username, password, loginCallback);
        }

The problem is that the old user still stays online! What is the best way to solve this? Or I am missing something? Right now I am going with manually setting user offline before Meteor.loginWithPassword call and setting it back online if callback return error which does not seem very elegant solution.


Solution

  • Ok thats what I did, not very happy with it but it works:

    'submit #loginForm': function(event) {
                event.preventDefault();
                var username = $("#usernameInput").val();
                var password = $("#passwordInput").val();
                var loginCallback = function (error) {
                    if (error) {
                        Meteor.call("goOnline");
                        FlashMessages.sendError(error.message);
                    } else {
                        FlashMessages.sendSuccess("Logged in as " + username + " successfully");
                        Meteor.logoutOtherClients(function(error) {
                            if (error) {
                                FlashMessages.sendError(error.message);
                            }
                        });
                    }
                };
                Meteor.call("goOffline"); // need to manually go offline as old user does not go offline after login
                Meteor.loginWithPassword(username, password, loginCallback);
                clearSession();
            }
    
    
    'goOffline': function () {
            Meteor.users.update({_id: this.userId}, {$set: {status: {online: false}}});
        },
    
        'goOnline': function () {
            Meteor.users.update({_id: this.userId}, {$set: {status: {online: true}}});
        },