Login on another application (server side)

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”?

1 Like

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.

2 Likes

get the return result

{
type: "password"
__proto__: Object
}

is success?

What exactly gave you this result? Was it calling the loginWithPassword method ? According to the documentation it should either doesn’t return anything or return an error if it failed to login.

You should be able to see if you were logged in successfully by running in your console:
Meteor.remoteUsers.user() (if you followed the steps from my first reply) which should give you the record of your user connected to the other server. You can also check that in backend logs or MongoDB users collection.

1 Like

thank you very much! you are right!
my code

let conn = new AccountsClient({
        connection: '',
        ddpUrl: 'localhost:4000'
      })
      console.log('--remoteLogin-cliet--',conn,Accounts._hashPassword('qq'));
      conn.callLoginMethod({
        methodArguments: [{
          user: {username: 'qq'},
          password: Accounts._hashPassword('qq')
        }],
        userCallback: function (error,result){
          if(error){
            alert('登录失败');
            console.log('error-失败:',error)
          }else{
            console.log('denglu-success',result,conn.user());
          }
        }
      })

i got the result i want.

{
username: "qq"
_id: "s7tGJeMLorLJBN9Nh"
}
1 Like