Meteor - Quick Tips #1

01/19/2015, Mon
Categories: #JavaScript
Tags: #Meteorjs

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

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

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,
  });
}