简体   繁体   中英

Meteor.user() always null

I'm a newbie JS developer and I'm trying to develop a Meteor app. I'm actually trying to use the Account package of Meteor. I've followed this tuto which is pretty clear : http://blog.benmcmahen.com/post/41741539120/building-a-customized-accounts-ui-for-meteor

Here is my login page (in Jade) :

template(name='login')
  div.container
    +menuLogin

    +form_signin


template(name="form_signin")
  form.form-signin(role="form" method="post" action="" id="form-signin" data-toggle="validator")
    h2.form-signin-heading Connexion
    input.form-control(type="text" placeholder="Nom d'utilisateur" name="userName" autofocus="" required )
    input.form-control(type="password" placeholder="Mot de passe" name="password" required )

    +profil_choice

    button.btn.btn-primary.btn-lg.btn-block.btn-connection(type="submit") Connexion
    div.alert.alert-danger.alert-dississible(id="alert-login" role="alert") 
        button.close(type="button" data-dismiss="alert")
            span(aria-hidden="true") ×
            span.sr-only Close
        p(id="alert-login-text")

And JavaScript :

Profils = new Mongo.Collection('profils');
Meteor.subscribe('profils');
Meteor.subscribe('users');

var userCursor = Meteor.users.find();
var handle = userCursor.observe({
  changed: function (tmp){
    console.log(tmp);
    Router.go('start');
  }
})

var trimInput = function(val){
  return val.replace(/^\s*|\s*$/g, "");
}

Meteor.autorun(function (){
  var message = Session.get('errorMessage');
  if (message){
    $('#alert-login-text').text(message);
    $('#alert-login').show(200).delay(3000).hide(200);
    Session.set('errorMessage', null);
  }
});

var checkLoginParams = function(userName, password, profil){
  if (profil == ""){
    Session.set('errorMessage', "Veuillez selectionner un profil !");
    return false;
  }
  return true;
}

Template.form_signin.events({
  'submit form': function (event, template){
    event.preventDefault();

    var userName = template.find("input[name=userName]").value;
    var password = template.find("input[name=password]").value;
    var profil = template.find("input[name=profilName]").value;

    var username = trimInput(userName);
    console.log("user : " + username + "  password : " + password);

    if (checkLoginParams(username, password, profil) == false){
      return;
    }

    Meteor.loginWithPassword(username, password, function (error){           
      if (error){
        Session.set('errorMessage', "Nom d'utilisateur ou mot de passe incorrect");
        console.log(error.reason);
      }
    });
    return false;
  }
});

I'm actually changing the template (with Iron router) when the Object user is changed on the database, because I couldn't find anything else to notice me wehther or not i'm logged in.

Router.route('login', function (){
  this.render('login');
});

Router.route('start',{
  path: 'start',
  onBeforeAction: function () {
    if (Meteor.user()){
      this.render('start');
    }
    else{
      if (Meteor.loggingIn()){
        this.render('start');
      }
      else{
        Router.go('login');
      }
    }
  }
});

The problem is, no matter where I call Meteor.user(), it's always null. So... I am apparently not connected.

There is only one user in the database and I'm sure that the connection parameters are good.

Moreover, why the hell can't I do a Router.go('start') in the loginWithPassword() callback ?

EDIT : Here is the creation of my user on the server side

if (Meteor.isServer) {
  Meteor.startup(function() 
  {
    Accounts.createUser({
        username : "admin",
        password : "123",
        email : "test@test.fr",
        isAdmin : true
  });
});

The problem was resolved by updating my version of Meteor. Actually... I don't really understand what was wrong.

You don't create user, so you can't login to account that doesn't exist http://docs.meteor.com/#/full/accounts_createuser

You could create callback for login like:

if (error)
        {
            console.log(error.reason);
        }

and erorr that user doesn't exist would pop-up

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM