Search code examples
javascriptmeteoriron-routermeteor-blaze

Meteor : redirect if user didn't create page


In Meteor I have a product edit page. I only want the user who created the product to see the page. Otherwise you get redirected.

Is there a way to do this redirect with iron router only? If not I'll take any solution.

router.js

var OnBeforeActions;
OnBeforeActions = {
    ownerRequired: function(pause){
      if(!Meteor.userId()){
        Router.go('home');
      }else if(Meteor.userId()._id != ....SOMETHING?....){
        Router.go('home');
      }else{
        this.next();
      }
    }
};

Router.onBeforeAction(OnBeforeActions.ownerRequired, {
    only: ['editProduct']
});


Router.route('/editProduct/:_id',{
  template: "editProduct",
  name: "editProduct",
  data: function(){
    return Products.findOne({_id: this.params._id});
  }
});

Solution

  • So if you want to know how to solve this without iron router. You could do something like this. (Note I already check with an onBeforeAction if the user is logged in.)

    Template.editProduct.onCreated(function(){
        if(this.data.user === Meteor.userId()){
        }else{
            Router.go('home');
        }
    });