简体   繁体   English

流星:如果用户未创建页面则重定向

[英]Meteor : redirect if user didn't create page

In Meteor I have a product edit page. 在Meteor中,我有一个产品编辑页面。 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 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});
  }
});

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.) (请注意,如果用户已登录,我已经使用onBeforeAction进行了检查。)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM