Meteor: enter DDP and fetch the current custom object in a separate Meteor app

First, a little background:

I am working on a separate mobile app that is linked to the main app. The connection has been successfully initiated and I can get all the collections using subscriptions:

Remote = DDP.connect('http://localhost:3000/');

Meteor.users = new Meteor.Collection('users', {
    connection: Remote
});

Remote.subscribe('users', true);

      

Now I want users to be able to login through the interface of the second application. After setting the password-password and the meteor-ddp-login package, I have to authenticate with the main application using the following code snippet on the client side.

var Remote = DDP.connect('http://localhost:3000/');

DDP.loginWithPassword(Remote, {
    username: username
}, password, function(error) {
    if (!error) {
        console.log(username + " is logged in!");
    } else {
        console.log(error);
    }
});

      

Ok, so far so good. No error appears and the console logs a successful message. Now the question arises:

How to get a custom user object that has just been logged in.

I have set up a few publish functions in the main app, but the user data is not made available to the client in the second app (other collections work fine, but Meteor.user () is undefined).

And also: How can I authenticate users who connect to Facebook / Google / Twitter

+3


source to share


1 answer


In this case, I have had a similar need lately. The following code works in Meteor version 1.2.0.2



    if (Meteor.isClient) {

      Meteor.startup (function () {
        // Seems that without this, on page refresh, it doesn't work.
        // COMMENT: Ideally this should not be needed if the core takes care of this use case of a different connection for Accounts
        // hack block 1 ***********
        var token = Accounts._storedLoginToken ();
         if (token) {
           Meteor.loginWithToken (token, function (err) {
            // this is going to throw error if we logged out
            if (err)
              console.log (err);
            else
              console.log ('loginWithToken');
          }); // loginWithToken
         }
        // hack block 1 ***********
      }); // startup function


      var connection = DDP.connect ("http: // localhost: 3060");
      Accounts.connection = connection;
      // COMMENT: Ideally this should not be needed if the core takes care of this use case of a different connection for Accounts
      // hack block 2 ***********
      Accounts.users = new Meteor.Collection ('users', {
          connection: connection
      });
      // hack block 2 ***********


      Tracker.autorun (function () {
        // No code which directly affects the functionality. Just for testing
        console.log (Meteor.user ());
        Accounts.connection.call ('user', function (err, result) {
          if (err)
            console.log (err);
          if (result) {
            console.log (result);
            if (result._id === Meteor.user () ._ id) {
              console.log ("Server and client shows that the same user has logged in");
            } else {console.log ("Server and client shows different users");}
          }
        })
      });

      Template.register.events ({
        'submit # register-form': function (e, t) {
          e.preventDefault ();
          var email = t.find ('# account-email'). value
            , password = t.find ('# account-password'). value;

          Accounts.createUser ({email: email, password: password}, ​​function (err, result) {
              if (err) {
                // Inform the user that account creation failed
                console.log (err);
              } else {
                // Success. Account has been created and the user
                // has logged in successfully.
                console.log ("registered user");
                console.log ('response is' + result);
                console.log (Meteor.user ());
              }
            }); // createUser
          return false;
        }
      }); // register

      Template.login.events ({
        'submit # login-form': function (e, t) {
          e.preventDefault ();
          var email = t.find ('# login-email'). value
          , password = t.find ('# login-password'). value;
            Meteor.loginWithPassword (email, password, function (err) {
            if (err)
                console.log (err);
            else
               // The user has been logged in.
                console.log ('logged in successfully');
            });
            return false;
        }
      }); // login

      Template.statusloggedin.events ({
        'click #logout': function (e, t) {
          e.preventDefault ();
          Meteor.logout ();
          return false;
        }
      }); // logout

    }

+1


source







All Articles