简体   繁体   中英

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 ".

  1. I added a package "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.

So below is the page returned when I login successfully.

在此处输入图片说明

How can I manage to redirect to correct path instead of 'notFound ' path ?

-------------UPDATED-------------

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

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.


Since you are using FlowRouter I would suggest following approach:

  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.

And the reason you got redirected to 404, I would guess that there is no route with name "client" defined.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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