Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meteor: Possible to check if application has already open instance on another computer?

Is it possible to check for an unique client in meteor? This sounds a little strange. Let me explain it:

I want to let my meteor app only work at one computer at the same time. But I can't use the IP for checking, as there are also computers in a same network, so there would be the same IP for the external sever.

If somebody open the app on a second computer, all other (opened) app instances on other computer should be logged out (or something like this).

Is this technical possible in meteor?

Update

Please note that I don't want to block a second login, but I want to logout on all other devices if the user do a login.

like image 329
user3142695 Avatar asked Oct 06 '15 07:10

user3142695


3 Answers

meteor has in built function for this

please Check

Meteor.logoutOtherClients([callback])

Meteor Documentation

like image 64
gatolgaj Avatar answered Oct 16 '22 09:10

gatolgaj


EDIT: I may have misunderstood the question, this is for making sure the user is only logged in at one device at a time. Not only one user at a time.

Not only is it possible, it's built in. Check out the docs

Call Meteor.logoutOtherClients() on login to logout other clients logged in as the same user.

/client/lib/hooks.js

Accounts.onLogin(function () {
    Meteor.logoutOtherClients(function (error) {
        if (error) {
            //Error handling goes here
            console.log(error)
        }

    })
}
like image 41
challett Avatar answered Oct 16 '22 08:10

challett


You need to use the Server callback validateLoginAttempt, and delete all the loginTokens from Meteor.users before logging in a user.

Example requires accounts-ui and accounts-password packages.

./single.html:

<head>
  <title>SingleUserSystem</title>
</head>

<body>
  <h1>Welcome to Meteor!</h1>
  {{> loginButtons}}
  {{> main}}
</body>

<template name="main">
  <div id="name">
    {{#if currentUser}}
      <h1>Logged In!</h1>
      <h3>Another user logging in will log you out.</h3>
    {{else}}
      <h1>Logged Out!</h1>
    {{/if}}
  </div>
</template>

./single.js:

if (Meteor.isServer) {
  // Server Callback on all login attempts
  Accounts.validateLoginAttempt(function(attempt) {
    if (!attempt.allowed)  // If login attempt was going to fail we
      return false;        // should not log out currently logged in user

    // deleting loginTokens from Meteor.users collection logs out users
    Meteor.users.update(
              {},
              {$set: { 'services.resume.loginTokens': [] } },
              {multi: true}
            );

    return true;   // return true to allow login to succeed
  });
}
like image 39
JeremyK Avatar answered Oct 16 '22 08:10

JeremyK