Meteor.user() is always null,Meteor.loggingIn() is false


#1

I am trying LDAP Login in my Meteor react app.meteor version 1.6.1 .Meteor.users collection is getting populated on server side but clientside when i check

if(Meteor.user()){
//user loginedin
}

Meteor.user() is null.so i cannot login to my app.
mongo collection users are getting populated.
I have published the users collection

Meteor.publish('users', function(id) {
  return Meteor.users.find({ _id: id });
});

and on my router:

Router.onBeforeAction(function () {
    if(!(Session.get("username"))) { //for time being to get into application i use session variable 
      console.log(Meteor.user(),"this is meteor user", Meteor.loggingIn(),Meteor.userId())
// but this is null
        this.layout("loginLayout");
        this.render('login');
   }else{

        this.layout("defaultLayout");
       this.next();
  }
})
Router.configure({
    layoutTemplate: 'loginLayout',

    waitOn: function() {
        return [

            Meteor.subscribe('users', Meteor.userId())

        ];
    }
});

I am using login mechanism on server as:

  if(authenication is success)
    try{

      var future=new Future();
      var userId = null;
      var displayName=""
      var stampedToken = {
        token: null
      };
      user=user.toLowerCase();
      // Look to see if user already exists
      var meteoruser = Meteor.users.findOne({
        username: user
      });

      // Login user if they exist
      if (meteoruser) {
        userId = meteoruser._id;


        // Create hashed token so user stays logged in
        stampedToken = Accounts._generateStampedLoginToken();
        var hashStampedToken = Accounts._hashStampedToken(stampedToken);
        // Update the user's token in mongo
        console.log("Update the user's token in mongo");
        Meteor.users.update(userId, {
          $push: {
            'services.resume.loginTokens': hashStampedToken
          }
        });
          displayName=Meteor.users.findOne({_id:userId}).profile.name


      }
      // Otherwise create user if option is set
      else  {
        console.log("user doesn't exist hense creating new user ");
        var uname = user;
        var userObject = {
          username: user,
          emails: [{
            address: uname,//***********************hardcoding********************
            verified: false
          }]
        };
        var ldapAsyncFut = new Future();
        userId = Accounts.createUser(userObject);
        console.log("my userID",userId)
        var ad = new ActiveDirectory({
          url: Meteor.settings.LDAP.LDAP_URL,
          baseDN: Meteor.settings.LDAP.LDAP_BASE_DN,
          username: user,
          password: password
        });
        ad.findUser(uname, Meteor.bindEnvironment(function(err, user) {
          if (err) {
            console.log('ERROR: ' +JSON.stringify(err));
            return;
          } if (! user) {
            console.log('User: ' + user+ ' not found.');
          }else{
            //console.log("user found : " +JSON.stringify(user));
            displayName=user.displayName
            console.log("user details for "+user.displayName+" found.creating profile in mongo db.");
            Meteor.users.update(userId, {
              $push: {
                emails: {
                  address: user.mail,
                  verified: false
                }
              },
              $set: {
                'profile.name': user.displayName
              }
            });
          }
        },function(e) {
          console.log("bind error", e.stack);
        }));
      }

      return {
        userId: userId,
        userName:displayName,
        token: stampedToken.token
      };

    }
    catch(e)
    {console.log("Exception Occured",e);}
  }