Login on another application (server side)


#1

Hey guys,
is it possible to connect to another Meteor application on server side and call the login method (f.e. loginWithPassword)? Currently I’m only able to establish a connection like this:

const ddp = DDP.connect("https://www.mainapp.com");

But how can I call a login method to have a “user connection”?


#2

You want to be able to connect and login to another Meteor server from the client perspective, right?

If yes then you have to create a new instance of AccountsClient and provide the created DDP connection to it.

For example:

Meteor.remoteConnection = DDP.connect('https://www.mainapp.com')
Meteor.remoteUsers = new AccountsClient({ connection: Meteor.remoteConnection })

After this you will have another working instance of accounts.

Unfortunately the instance won’t give you a method like Meteor.loginWithPassword.
You can check the implementation of the original method here: Meteor source code

Since I also wanted to be able to have loginWithPassword method for my secondary accounts I just copied the existing implementation and changed the Accounts.callLoginMethod to my Meteor.remoteUsers.callLoginMethod.

The result is below:

Meteor.remoteConnection.loginWithPassword = function (selector, password, callback) {
  if (typeof selector === 'string')
    if (selector.indexOf('@') === -1)
      selector = {username: selector};
    else
      selector = {email: selector};

  Meteor.remoteUsers.callLoginMethod({
    methodArguments: [{
      user: selector,
      password: Accounts._hashPassword(password)
    }],
    userCallback: function (error, result) {
      if (error && error.error === 400 &&
          error.reason === 'old password format') {
        srpUpgradePath({
          upgradeError: error,
          userSelector: selector,
          plaintextPassword: password
        }, callback);
      }
      else if (error) {
        reportError(error, callback);
      } else {
        callback && callback();
      }
    }
  });
};

All of what I wrote has to be done only on the client side.
You can give it a try and let me know if that is fulfilling your needs.