Best way to do common method checks? ( user is logged in )

Almost in every meteor method i have these sort of checks:

if (!Meteor.userId()) throw new Meteor.Error('not-authorized')

This goes against DRY principle, and i would like to get rid of it somehow to make code more readable.

By the way, do i need to make this check if i have configured Simple Schema? Like this? -

`Collection.schema = new SimpleSchema({
	userId: {
                label: 'document userId',
		type: String,
		regEx: SimpleSchema.RegEx.Id,
		autoValue() { if ( this.isInsert ) return this.userId }
	}
})`

If you are using validated methods, which is the current recommendation, then there are a host of mixins available. One of them would be what you are looking for.

Personally I use all client side minimongo operations and secure it with Simple Schema and light allow/deny rules. I find this to be a much nicer development experience.

1 Like