4

First a little background:

I am working on an seperate mobile application that is connected with the main app. The connection is succesfully initiated and I can retrieve all collections, through subscriptions:

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

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

Remote.subscribe('users', true);

Now I want to make sure users can log in through the interface of the second app. After installing the accounts-password and the meteor-ddp-login package, I should be able to authenticate with the main app by using the next piece of code in 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);
    }
});

Well, so far so good. No errors appear and the console logs a success message. Now the question comes:

How can I retrieve the user object of the user who just logged in.

I've set up several publish functions in the main app, but the user data does not become 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 login with Facebook/Google/Twitter

Fullhdpixel
  • 803
  • 1
  • 13
  • 28

1 Answers1

1

Came across this, I had a similar need recently. 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

    }