Search code examples
javascriptmeteormeteor-accountsmeteor-helpermeteor-publications

Meteor: Accounts.createUser() doesn't create user


i'm building an application where people Can't create their account by themselves but the first user created (me) can create users in a form in the application.

It's why I setted in /lib/config/account.js: forbidClientAccountCreation: true,

My problem is, I can't create users in my form when I'm logged .. (even if I set this option above with false. Here is my code:

userAdd.js:

Template.userAdd.events({
'submit .new-user': function(event) {
    event.preventDefault();

    var email = $('input[name="email"]').val();;
    var username = $('input[name="username"]').val();;

    Accounts.createUser({
        email: email,
        username: username,
        password: 'toto',
        profile: {

        }
    });

    event.target.email.value = "";
    event.target.username.value = "";
}
});

/server/publications/user.js:

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

 Meteor.users.allow({
   'insert': function (userId, doc) {
     return true; 
   }
 });

My userList where I display the list of the users:

Meteor.subscribe('users');

Template.usersList.helpers({
   users: function() {
     return Meteor.users.find();
   }
});

I tried also with Meteor.users.insert() but it doesn't work too ..

Could you help me please ?


Added the Routes:

FlowRouter.route('/users', {
name: "users",
 action: function() {
    BlazeLayout.render('applicationLayout', {
        menu: 'menu',
        container: 'usersList'
    });
 }
});

AccountsTemplates.configureRoute('changePwd');
AccountsTemplates.configureRoute('forgotPwd');
AccountsTemplates.configureRoute('resetPwd');
AccountsTemplates.configureRoute('signIn');

AddUser.html

<template name="userAdd">
  <form class="ui form new-user">
    <div class="inline fields">
        <div class="seven wide field">
            <label>Email</label>
            <input type="text" name="email" placeholder="Email">
        </div>
        <div class="seven wide field">
            <label>Username</label>
            <input type="text" name="username" placeholder="Username">
        </div>
        <button class="ui button icon right labeled teal" type="submit" name="submit"><i class="right checkmark icon"></i>Valider</button>
    </div>
  </form>
</template>

user.js

Template.tableUser.helpers({
  email: function() {
    return this.emails[0].address; 
  }
});

.Packages

insecure                # Allow all DB writes from clients (for prototyping)
kadira:flow-router
semantic:ui
flemay:less-autoprefixer
underscore
kadira:blaze-layout
arillo:flow-router-helpers
zimme:active-route
aldeed:collection2
accounts-password
useraccounts:flow-routing
useraccounts:semantic-ui
accounts-base

That's all related to the Accounts...


Solution

  • Thanks @JeremyK and @Sasikanth! It's working now,

    I changed my code to server side. Don't know if all is perfect but here is the working code:

    addUser.js:

    Meteor.subscribe('users');
    
    Template.userAdd.events({
     'submit .new-user': function(event) {
        event.preventDefault();
    
        var email = $('input[name="email"]').val();
        var password = $('input[name="password"]').val();
        var firstname = $('input[name="firstname"]').val();
        var lastname = $('input[name="lastname"]').val();
    
        Meteor.call("createUsers", email, password, firstname, lastname);
    
        event.target.email.value = "";
        event.target.password.value = "";
        event.target.firstname.value = "";
        event.target.lastname.value = "";
     }
    });
    

    /server/methods/user.js

    Meteor.methods({
     createUsers: function(email, password, firstname, lastname) {
        Accounts.createUser({
            password: password,
            username: firstname + ' ' + lastname,
            email: email,
            createdAt: new Date(),
        });
     },
    
     deleteUser : function(id){
        return Meteor.users.remove(id);
     },
    });
    

    user.js

    Template.tableUser.events({
      "click .delete": function () {
        var idUser= this._id;
        Meteor.call('deleteUser',{_id:idUser})
      }
    });
    

    Thank you very much :)