用户刚刚登录或刚刚注销后如何重定向

lle*_*188 8 meteor iron-router

似乎Deps.autorun是要走的路,但是Router.go似乎在Deps.autorun中不起作用.

Dav*_*don 17

这里是三条路线的例子:index,signindashboard:

Router.configure({layoutTemplate: 'layout'});

Router.map(function() {
  this.route('index', {path: '/'});
  this.route('signin');
  this.route('dashboard');
});

var mustBeSignedIn = function(pause) {
  if (!(Meteor.user() || Meteor.loggingIn())) {
    Router.go('signin');
  } else {
    this.next();
  }
};

var goToDashboard = function(pause) {
  if (Meteor.user()) {
    Router.go('dashboard');
  } else {
    this.next();
  }
};

Router.onBeforeAction(mustBeSignedIn, {except: ['signin']});
Router.onBeforeAction(goToDashboard, {only: ['index']});
Run Code Online (Sandbox Code Playgroud)

如果用户在index页面上并且她已登录,则她将自动路由到该dashboard页面.在除了之外的任何页面上signin,如果用户未登录,她将被路由到该signin页面.onBeforeAction是反应性的,因此如果用户登录或退出,将立即强制执行这些规则.

当然你的路线会有所不同,但希望这个例子说明了使用铁路由器实现这一功能的一种方法.

另请参阅铁路由器导轨使用挂钩部分.


Mar*_*ust 7

上面的一些事情似乎已经过时了.以下是我现在的工作方式:

Router.configure({
    layoutTemplate: 'Layout'
});

Router.map(function() {
    this.route('index', {path: '/'});
    this.route('login');
    this.route('home');
});

var mustBeSignedIn = function() {
    if (!(Meteor.user() || Meteor.loggingIn())) {
        Router.go('login');
    } else {
        this.next();
    }
};
var goHome = function() {
    if (Meteor.user()) {
        Router.go('home');
    } else {
        this.next();
    }
};

Router.onBeforeAction(mustBeSignedIn, {except: ['login']});
Router.onBeforeAction(goHome, {only: ['index', 'login']});
Run Code Online (Sandbox Code Playgroud)