用户使用铁路由器登录后如何重定向

How to redirect after user log in with Iron Router?

本文关键字:重定向 登录 路由器 用户      更新时间:2023-09-26

我想在用户登录时重定向到"/"主页,我设法使用 this.render 重定向到"/"主页,但随后它只显示主页,无法访问其他网址。

Router.route('/', {name: 'yaksList'})
Router.route('/submit', {name: 'yaksSubmit'});
Router.route('/login', {name: 'accounts'});
Router.onBeforeAction(function() {
  if (Meteor.userId()) {
    this.render('yaksList');
  } else {
    this.next();
  }
});

我尝试使用Router.go('yaksList')而不是this.render,但它不起作用。请问正确的解决方案是什么?

这里有一个关于如何处理受限路由的精细描述。

Schoebel先生的方法让您需要登录才能访问某些路由。如果您尝试直接访问受限制的路线,它会在两者之间放置您的登录页面,然后将您带到那里。

您可以使用currentUser助手在yaksList template上做一些简单的事情。

{{#if currentUser}}
   {{> yaksList}}
{{else}}
   {{> login}}
{{/if}}

或使用 router.go .

Router.go('yaksList');//在提交按钮上。

更新

但是当他们想对帖子投赞成票/反对票时,他们需要登录

对事件处理程序使用验证。

Template.yaksLists.events({
  'click #upVote':function(event,template){
    //some other code here
    //validation
    if(!Meteor.userId()){
      //here you redirect the user to the /login
      Router.go('/login');
     }else{
      //do the up vote stuff
     }
   }
})