meteor / validated-method

Meteor methods with better scoping, argument checking, and good defaults.
https://atmospherejs.com/mdg/validated-method
MIT License
194 stars 28 forks source link

Using 1.3 modules to load server code from Method run function #40

Closed nabiltntn closed 8 years ago

nabiltntn commented 8 years ago

As recommended in the Meteor guide, it's better for security reasons to define sensitive code in server side and call it in method code in Meteor.isServerblock. How it's possible to apply such recommendations with Meteor 1.3 with the given example. Suppose we have under both folder the given method :

import {MailingUtils} from '/server/mailing.js';
export const AppMethods = {
    updateUser: new ValidatedMethod({
        name: 'updateUser',
        validate: null,
        run() {
            console.log('calling the update method');
            if (Meteor.isServer) {
                MailingUtils.sendSecretMail();
            }
        }
    })
}

And under server folder the actual implementation of the sensitive code

export const MailingUtils = {
    sendSecretMail() {
        console.log('sending secret mail');
    }
}

On the browser console, i have and error message loading the /server/mailing.js module

stubailo commented 8 years ago

@nabiltntn you need to use require inside an if like so:

if (Meteor.isServer) {
  const { MailingUtils } = require('/server/mailing.js');
  // do stuff
}

This is because according to the ES2015 standard you aren't allowed to have import statements that aren't at the root of the file.

@benjamn, did I get it right?

@tmeasday, we should add this to the guide when we migrate the code samples to 1.3.

stubailo commented 8 years ago

Closing in favor of: https://github.com/meteor/guide/issues/338