1
FlowRouter.go(redirect); //gets triggered, but site does not actually redirect until refreshed.

我按照本指南来构建我的路线:

var authorised = FlowRouter.group();

var publicRoutes = FlowRouter.group();

FlowRouter.triggers.enter([isUserSignedIn]);

authorised.route('/',{
  name:'root',
  action(){
    BlazeLayout.render('App_body',{ main: 'App_home'});
  }
});

publicRoutes.route('/welcome',{
  name : 'welcome',
  action(){
    BlazeLayout.render('Unauthorised', { main: 'welcome' });
  }
});


function isUserSignedIn(){
  if (!Meteor.user() || Meteor.loggingIn()){
    var route = FlowRouter.current();
    if (route.path != "/welcome") {
      // Set Session to redirect path after login
      Session.set("redirectAfterLogin", route.path);
    }
    console.log("user is not signed in");
    FlowRouter.go('welcome');
  }
};

// Redirect After Login
Accounts.onLogin(function(){
  console.log("Accounts.onLogin()");
  var redirect = Session.get("redirectAfterLogin");
  if (redirect){
    console.log("redirect path exists")
    if(redirect != "/welcome"){
      console.log("redirect is not welcome path, redirect to ", redirect);
      FlowRouter.go(Session.get("redirectAfterLogin"));
    }
  }
  else{
    // if redirect doesn't exist, go "/"
    console.log("no redirection, go root");
    FlowRouter.go('root');
  }
})

// Not Found 
FlowRouter.notFound = {
  action() {
    BlazeLayout.render('Unauthorised', { main: 'App_notFound' });
  },
};

上面的代码执行以下操作:

案例1:强制Session.set("redirectAfterLogin", "/blah");

  1. 注销应用程序。
  2. 在控制台输入Session.set("redirectAfterLogin", "/blah");
  3. 登录
  4. 在控制台中观察以下输出:
    • Accounts.onLogin()
    • redirect path exists
    • redirect is not welcome path, redirect to /blah

但我仍然使用“未经授权”的布局,带有“欢迎”模板”。

  1. 按刷新,我被重定向到“未找到”——这是正确的结果。

案例 2: Session.get("redirectAfterLogin") 未定义

  1. 注销应用程序。
  2. 在控制台输入Session.set("redirectAfterLogin");
  3. 登录
  4. 在控制台中观察以下输出:
    • Accounts.onLogin()
    • no redirection, go root

但我仍然使用“未经授权”的布局,带有“欢迎”模板”。

  1. 按刷新,我被重定向到“/” - 这是正确的结果。

究竟是什么阻碍了这里的逻辑?请帮忙!

4

2 回答 2

0

这就是我的问题的原因,角色没有正确订阅,因此我被困在了unauthorised布局上。我希望这有帮助!

FlowRouter.wait()
// Tracker.autorun ->
//   # if the roles subscription is ready, start routing
//   # there are specific cases that this reruns, so we also check
//   # that FlowRouter hasn't initalized already
//   if Roles.subscription.ready() and !FlowRouter._initialized
//      FlowRouter.initialize()
Tracker.autorun(function(){
  if (Roles.subscription.ready() && !FlowRouter._initialized){
    FlowRouter.initialize();
  }
});
于 2017-11-21T09:49:09.343 回答
0

在事件中注销后尝试重定向时遇到了这个问题:

'click .js-logout'() {
    Meteor.logout();

    FlowRouter.go('signin');
},

我的快速解决方案是为调用路由器添加超时:

'click .js-logout'() {
    Meteor.logout();

    // we have to do redirect a bit later because logout is interfering the redirection
    setTimeout( 
      () => {
        FlowRouter.go('signin');
      }, 100
    );
 },

您可能想要增加超时。

于 2017-11-17T07:44:04.490 回答