Digits, Part 8

Up to now, the Digits system has used the “insecure” package, which enables clients to update the Mongo database in any way they want. In this experience, you’ll remove the insecure package and replace the calls in your client code with Meteor method invocations. This allows you to control the way a client modifies the database.

Before starting this WOD, please read through Meteor Tutorial: Security with methods. You may also find Meteor Guide: Methods useful to skim before starting this WOD.

As with the earlier Digits WODs, feel free to watch me solve it first, and then try it yourself.

Before starting this WOD, be sure to have merged the branch containing the “final” version of your previous WOD into master. See the screencast in the Readings for an illustration.

Note that switching branches on a running meteor application has the potential to put the application into an inconsistent state. To minimize problems, whenever you switch branches while doing meteor development, it is best to:

  1. Quit meteor.
  2. Invoke meteor reset to clear out the contents of the database.
  3. Re-invoke meteor. For applications based upon meteor-application-template, the correct invocation is meteor --settings ../config/settings.development.json.

The WOD

Ready? Let’s begin:

  1. Start your timer.

  2. Create and switch to a branch called “methods-1” in your local repository. You will do all the work for this WOD in this branch.

  3. Run meteor remove insecure, and then meteor --settings ../config/settings.development.json. After logging in, you will find that while you can see the Contacts associated with a user, you can no longer add new Contacts or edit existing ones.

  4. Following the approach in Meteor Tutorial: Security with methods, create a method named “contacts.insert”. This method should:

    • Use the ‘check’ method to ensure that the passed contact is an Object.
    • Throw a Meteor.Error if the user is not signed in or the signed in user is not listed as the owner of the Contact.
    • Validate the object against the Contact schema.
    • If valid, then insert the object, else throw a Meteor.Error.

    Since this is a bit complicated to figure out the first time, here is example code for the insert method:

     'contacts.insert': function insertContact(contact) {
       check(contact, Object);
       if (!this.userId || this.userId !== contact.owner) {
         throw new Meteor.Error('Not signed in or attempting to add a contact you do not own.');
       }
       const schemaContext = ContactsSchema.namedContext('contacts.insert');
       schemaContext.resetValidation();
       schemaContext.validate(contact);
       if (schemaContext.isValid()) {
         Contacts.insert(contact);
       } else {
         throw new Meteor.Error('Invalid contact object.');
       }
     },
    
  5. Next, you need to call the contacts.insert method in add-contact-page.js. You need to replace the insert call on the Contacts collection with a call to the ‘contacts.insert’ method, and provide a callback method that runs when the method completes. Again, this is a little complicated the first time, so here’s example code:

    Meteor.call('contacts.insert', newContact, (err) => {
      if (!err) {
        instance.messageFlags.set(displayErrorMessages, false);
        FlowRouter.go('Home_Page');
      }
    });
    
  6. Now check to see that you can add a new Contact successfully.

  7. Verify that the owner field is added to the updatedContact object in edit-contact-page.js. (This was not shown in Digits 7.)

  8. Create a contacts.update method and invoke it within edit-contact-page.js. Note that this method must be passed thethe ID of the Contact to be updated as well as the object containing the updated values. Use Destructured parameters to accomplish this easily.

  9. Test that Contacts can now be updated successfully.

  10. Once you’ve finished, commit your changes to GitHub, and check to see that your changes are there.

  11. Stop your timer and record your time. Be sure to record it, because you will need your WOD time data when you write your technical essay.

Rx: <23 min Av: 23-30 min Sd: 30-35 min DNF: 35+ min

Demonstration

You can watch this before doing the WOD if you like:

If you want to try this WOD again, just commit your branch, then switch to the master branch to reset your system to its state at the end of the first Digits experience. Then create a new branch called multiuser-2 and start over.

Submission instructions

This WOD does not need to be submitted. It is just for review purposes.