How can I disconnect a user from server-side code?

I want to immediately kill his session, forcing him to login again.

I’ve tried clearing the login tokens, but that doesn’t kill active sessions.

Tx,
G

Are you looking for Meteor.logout() ? http://docs.meteor.com/#/full/meteor_logout

1 Like

That’s a client-side only method

If using a method initiated from the client you can do this.userId = null

I ended-up using this:

closeAllUserSessions = function(userId) {

    var sessions = _.filter(Meteor.default_server.sessions, function (session) {

        return session.userId == userId;

    });

    _.each(sessions, function (session) {

        session.connectionHandle.close();

    });

}
1 Like

Seems like you already came up with a good solution. But even if it is a client only method, you can send a command from the server to the client to run that method on the client. No?

I don’t know of such an option. Could you give a code example?

This worked for me, following example:

import { Meteor }   from 'meteor/meteor';

//meteor add accounts-base
import { Accounts } from 'meteor/accounts-base'

Meteor.methods ({
  forceLogout: function () {
    if (this.userId) {
      Accounts._server.method_handlers.logout ();
      Accounts._server.method_handlers.logoutOtherClients ();
    }
  }
});

Hey, has anyone found a good solution for this yet? I’m trying to log out user from the server… seems this should be a rather simple task and I’m just hours banging my head :frowning:

I tried both methods on this thread:
Using connectionHandle.close(); on all user sessions resulted in a never-ending loop where the client keeps logging the user back in…

Using Accounts._server.method_handlers.logout (); results in a server error: id is not defined

can anyone here help me with this?

thanks alot

Try this:

Meteor.users.update({ _id: theUserId }, { $set: { "services.resume.loginTokens": [] } });
```
1 Like

How about

this.setUserId(null)

It only works when you are calling a method though, but it is server side.

Hey thx, I’ve tried this and it doesn’t seem to logout the user… even after page refresh the user is still logged in.

Hi thx, this is actually the only way I successfully logged users out, but for some reason I started getting a lot of feedback from users, getting kicked out of the system for no reason… not sure if clearing the loginTokens array is the cause but I’m still debugging…