简体   繁体   English

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

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

All my routes are working fine and my routes are declared in " D:\\test\\imports\\startup\\client\\routes.js ". 我所有的路由都工作正常,并且在“ D:\\ test \\ imports \\ startup \\ client \\ routes.js ”中声明了我的路由。

  1. I added a package "gwendall:auth-client-callbacks". 我添加了一个软件包“ gwendall:auth-client-callbacks”。
  2. I added below piece of code at the end of all routes. 我在所有路由的末尾添加了以下代码。

      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('/'); }); 

While the logout module runs perfect, the onLogin() call redirects to page but with " FlowRouter.notFound " action. 当注销模块运行完美时,onLogin()调用将重定向到页面,但动作为“ FlowRouter.notFound ”。

So below is the page returned when I login successfully. 因此,下面是我成功登录后返回的页面。

在此处输入图片说明

How can I manage to redirect to correct path instead of 'notFound ' path ? 如何管理重定向到正确的路径而不是“ notFound ”路径?

-------------UPDATED------------- - - - - - - -更新 - - - - - - -

PROJECT\\imports\\startup\\client\\routes.js 项目\\导入\\启动\\客户端\\ 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'});
  }
});

UPDATE 更新

After question was updated it appeared that client was the name of the FlowRouter group instead of route name, which is client-dashboard and should be used to redirect user. 更新问题后,似乎client是FlowRouter组的名称,而不是路由名称,该名称是client-dashboard ,应用于重定向用户。


Since you are using FlowRouter I would suggest following approach: 由于您使用的是FlowRouter,因此我建议采用以下方法:

  1. Create two routes groups: 创建两个路由组:

     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. Then you define your routes respectively: 然后分别定义路线:

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

Thus, FlowRouter will handle redirection based on Meteor.userId cookie value. 因此,FlowRouter将基于Meteor.userId cookie值处理重定向。

And the reason you got redirected to 404, I would guess that there is no route with name "client" defined. 以及您被重定向到404的原因,我想可能没有定义名称“ client”的路由。

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

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