Meteor - Quick Tips #1

Redirecting for Non-Logged-In Users

When an app has more than one url and any other url other than the /home url is restricted for logged in users, than this requireLogin function will direct users back to the home url when the user is not logged in. This is assuming that the IronRouter package is used.

Force Login for Privileged Areas
1
2
3
4
5
6
7
8
9
10
11
var requireLogin = function() {
if(!Meteor.user()) {
if(!Meteor.loggingIn()){
this.redirect('home');
}
} else {
this.next();
}
}

Router.onBeforeAction(requireLogin, {except: 'home'});

JSON Data for Fixtures

Assuming that “exercise.json” is stored in your /private directory in the root of the Meteor project, the fixture.js file in your /server folder makes use of Assets.getText in conjunction with JSON.parse to get the data in a form that can be readily accessed.

Get Private JSON Data
1
2
3
4
5
6
7
8
9
10
var exercises = JSON.parse(Assets.getText("exercises.json"));

for (var i = 0, len = exercises.length; i < len; i++) {
var exercise = exercises[i];

Exercises.insert({
name: exercise.name,
type: exercise.type
});
}