Adding field to Meteor.user


#1

I’m trying to add a field to the user collection admin as a boolean that will track if a user is an admin or not.

I made the schema as such.

UserSchema = new SimpleSchema({
    _id: {
        type: String,
        regEx: SimpleSchema.RegEx.Id
    },
    emails: {
        type: Array,
    },
    username: {
        type: String,
        optional:true
    },
    "emails.$": {
        type: Object,
        blackbox: true
    },
    "emails.$.address": {
        type: String,
        regEx: SimpleSchema.RegEx.Email
    },
    "emails.$.verified": {
        type: Boolean,
        optional: true
    },
    profile: {
        type: ParentProfile,
        optional:true
    },
    services: {
        type: Object,
        optional: true,
        blackbox: true
    },
    admin:{
      type: Boolean
    }
});

I want this on default to set to false so I have it set on onCreated user as such:

  Accounts.onCreateUser(function (options, user) {

      if (user.services.google) {
        user.username = user.services.google.name;
        user.emails = [{address: user.services.google.email}];
      }
      else if(user.services.facebook) {
        user.username = user.services.facebook.name;
        user.emails = [{address: user.services.facebook.email}];
      }

      user.admin = false;

      return user;
  });


});

However on creation it does not add the admin field, but it does do the username portion correctly.

Also I have put in a button that should add the admin field and set it to true… but it only appears to set it temporarily and then it seems to loose the admin field all together again

Template.ShowUser.events({
  'click .makeAdmin' : () => {
    Meteor.users.update(this.id, {$set : { admin: true }});
    console.log(Meteor.user());
  }
});

So when it prints the user here it shows the admin is set to true… but then when i go back to the profile the admin field as disappeared again.

What am i missing here?

Also I tried to jsut put it as a field in their profile… but when using the autoform it didnt seem to want to set the auto or default value and thus would not add the record unless i left the field optional. This is the profile schema I am using:

ParentProfile = new SimpleSchema({
    firstName: {
      type: String,
      label: "First Name"
    },
    lastName: {
      type: String,
      label: "Last Name"
    },
    birthday:{
      type: Date,
      autoform: {
        type: "bootstrap-datepicker",
        datePickerOptions: {
          autoclose: true
        }
      }
    },
    zipcode:{
      type: String,
      label: "Zip Code",
      autoform:{
        mask: '00000',
        type: 'masked-input',
        placeholder: '00000'
      }
    },
    admin:{
      type: String,
      optional: true,
      autoform:{
        type: "hidden"
      },
      defaultValue: "no",
      autoValue: () => {
        return "no";
      }
    }
},{
  clean:{
    filter: true,
    autoConvert: true,
    removeEmptyStrings: true,
    getAutoValues: true,
  }

});

Autoform seems to be missing alot of functionality, is there a better package to be using? O r should I just be doing all of this myself? I was trying to use simple-schema and autoform to make it easier but it just seems to not want to work properly for anything advanced, or am I missing something?

I tried to set the form to clean before submitting but that functionality still did not solve the problem, it seems this may be an issue with autoform and nested objects?

Can someone please give some advice on how to solve this issue?


#2

First of all this may be suitable for the alanning-roles packages which will eventually allow you to define far more granular roles.

This is because you have added this code in the client section (Blaze templates) only. The client does not validate the change and applies it, the user is then refreshed from the server and you have not set this value there.

Have a look at Meteor.methods / Meteor.call or the validated-method package and learn how to define code on the server.


#3

So i tried to implement that but still no go it seems::

I made a meteor Method server side:

  setAdmin(id){
    Meteor.users.update(id, {$set : { isAdmin: true }});
  },

And im doing a meteor call now client side:

    Meteor.call('setAdmin', this.id, ()=>{
      console.log(Meteor.user());
    });

the field is not adding, and is still not adding upon create user which i don’t understand.


#4

I can’t see any trace of a publication publishing the extra fields to the client. The client would also need to subscribe to the publication to be able to see the extra field.

The users collection is a bit special in meteor is protected for security purposes. I suspect if you look directly at the mongo collection that the extra field may be being populated. It just the client that cannot see it.

Have a look at the meteor guide https://guide.meteor.com/accounts.html#publish-custom-data