Account.onLogin() 上的 FlowRouter 在 Meteor 中无法正确重定向

FlowRouter on Account.onLogin() doesn't redirect properly in Meteor

我所有的路由都工作正常,我的路由在“D:\test\imports\startup\client\routes.js”中声明。

  1. 我添加了一个包"gwendall:auth-client-callbacks"。
  2. 我在所有路由的末尾添加了以下代码。

     Accounts.onLogin(function(){
       console.log(Meteor.user().profile.customerType == 'CLIENT'); // shows true
       if(Meteor.user().profile.customerType == 'CLIENT'){
          FlowRouter.go('client');
       } else {
          console.log('else');
       }
    });
    
    Accounts.onLogout(function(){
      FlowRouter.go('/');
    });
    

虽然注销模块运行完美,但 onLogin() 调用重定向到页面,但带有“FlowRouter.notFound”操作。

下面是我登录成功后返回的页面

我如何设法重定向到正确的路径而不是 'notFound' 路径?

------------更新------------

PROJECT\imports\startup\client\routes.js

var clientRoutes = FlowRouter.group({
  prefix: '/client',
  name: 'client',
  triggersEnter: [function(context, redirect) {
    if(!Meteor.userId()){FlowRouter.go('/');}

    console.log('/Client route called.');
  }]
});

clientRoutes.route('/', {
  name: 'client-dashboard',
  action: function() {
    console.log("Dashboard called.");
    BlazeLayout.render('App_AuthClient', { main : 'App_UserDashboard'});
  }
});

更新

问题更新后,似乎 client 是 Fl​​owRouter 组的名称而不是路由名称,路由名称是 client-dashboard,应该用于重定向用户。


由于您使用的是 FlowRouter,因此我建议采用以下方法:

  1. 创建两个路由组:

    const protectedRoutesGroup = FlowRouter.group({
        triggersEnter: [function () {
            if (!Meteor.userId) {
                FlowRouter.go('login')
            }
        }]
    });
    
    const guestOnlyRoutesGroup = FlowRouter.group({
        triggersEnter: [function () {
            if (Meteor.userId) {
                FlowRouter.go('homepage')
            }
        }]
    });
    
  2. 然后你分别定义你的路由:

    protectedRoutesGroup.route('/', {
        name: 'homepage',
        action () {
            BlazeLayout.render(baseLayout, { main: 'template_name' });
        }
    });
    
    guestOnlyRoutesGroup.route('/', {
        name: 'login',
        action () {
            BlazeLayout.render(baseLayout, { main: 'login_template' });
        }
    });
    

因此,FlowRouter 将根据 Meteor.userId cookie 值处理重定向。

以及您被重定向到 404 的原因,我猜想没有定义名称 "client" 的路由。