var isSignedIn = function () {
  if (!Meteor.userId()) {
    Router.go('user.login', {}, {
      query: { redirect: this.url }
    });
  }
  this.next();
}

Router.onBeforeAction(isSignedIn, {
  except: ['user.login', 'user.register', 'user.forgotPassword', 'user.resetPassword']
});

UserRouteController = RouteController.extend({
});

Router.route('/login', {
  name: 'user.login',
  controller: UserRouteController
});

Router.route('/logout', function () {
  Meteor.logout();
  Router.go('content.index');
}, {
  name: 'user.logout'
});

Router.route('/register', {
  name: 'user.register',
  controller: UserRouteController
});

Router.route('/forgot-password', {
  name: 'user.forgotPassword',
  controller: UserRouteController
});

Router.route('/reset-password/:token?', {
  name: 'user.resetPassword',
  controller: UserRouteController
});

Router.route('/profile/:_id?', {
  name: 'user.profile',
  controller: UserRouteController,
  waitOn: function () {
    if(this.params._id)
      return [Meteor.subscribe('user', this.params._id), Meteor.subscribe('user')];
    else
      return Meteor.subscribe('user');
  },
  data: function () {
    var data = {};
    if(this.params._id)
      data.user = Meteor.users.findOne(this.params._id);
    else
      data.user = Meteor.user();
    return data;
  }
});
