Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add/edit users with meteor accounts and autoform

I am building part of an admin system in Meteor that lets admins add/edit other admins. I am using Meteor Accounts and Autoform, but I can't figure out how to handle it so the users are validated with Autoform and saved properly. From what i've found it looks like I need to use the Accounts.createUser method and make the form a type="method" or something, but I'm not sure how to handle that or if that is even the correct way.

Here is my code right now:

Schema:

Schema = {};

Schema.UserProfile = new SimpleSchema({
    name: {
        type: String,
        label: "Name"
    }
});

Schema.User = new SimpleSchema({
    email: {
        type: String,
        regEx: SimpleSchema.RegEx.Email
    },
    password: {
      type: String,
      label: "Password",
      min: 6
    },
    passwordConfirmation: {
      type: String,
      min: 6,
      label: "Password Confirmation",
      custom: function() {
        if (this.value !== this.field('password').value) {
          return "passwordMissmatch";
        }
      }
    },
    createdAt: {
      type: Date,
      autoValue: function() {
        if (this.isInsert) {
          return new Date;
        } else if (this.isUpsert) {
          return {$setOnInsert: new Date};
        } else {
          this.unset();
        }
      }
    },
    profile: {
        type: Schema.UserProfile
    },
    services: {
        type: Object,
        optional: true,
        blackbox: false
    }
});

Meteor.users.attachSchema(Schema.User);

Routes:

Router.route('/admin/admins', {
    controller: 'AdminController',
  name: 'adminAdmins',
  title: 'Admins',
  parent: 'adminHome',
});

Router.route('/admin/admins/new', {
    controller: 'AdminController',
    name: 'adminAdminNew',
    title: 'New Admin',
    parent: 'adminAdmins',
});

Router.route('/admin/admins/:_id/edit', {
    controller: 'AdminController',
  name: 'adminAdminEdit',
  title: 'Edit Admin',
    parent: 'adminAdmins',
    data: function() {
        return Meteor.users.findOne(this.params._id);
    }
});

Admin Form:

{{#autoForm collection="Meteor.users" doc=this id="adminAdminForm" type=formType}}

    {{> afQuickField name='profile.name'}}
    {{> afQuickField name='email'}}
    {{> afQuickField name='password'}}
    {{> afQuickField name='passwordConfirmation'}}

    <button type="submit" class="btn btn-block btn-secondary">Save Changes</button>
{{/autoForm}}
like image 262
NineBlindEyes Avatar asked Jan 15 '15 03:01

NineBlindEyes


Video Answer


1 Answers

You should add Hooks to be able to modify the collection Something that should look like this

AutoForm.hooks({
  adminAdminForm: {
    onSubmit: function (doc) {
        schemas.User.clean(doc);
        this.done();
        return false;
    },
    onSuccess:function(operation, result, template){
        Router.go('users.show',{'username':template.data.doc.username});
    },
    onError: function(operation, error, template) {
        console.log(operation,error)
    }
  }
});

You can find more details on the dedicated documentation https://github.com/aldeed/meteor-autoform#callbackshooks

like image 90
Nicolas Grenié Avatar answered Nov 02 '22 20:11

Nicolas Grenié