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

I followed this guide to structure my routes:

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

The code above does the following:

Case 1: Force Session.set("redirectAfterLogin", "/blah");

  1. Logout of the app.
  2. Enter in console Session.set("redirectAfterLogin", "/blah");
  3. Login
  4. Observe the following output in console:
    • Accounts.onLogin()
    • redirect path exists
    • redirect is not welcome path, redirect to /blah

But I am still on the "Unauthorised" Layout, with "welcome" template".

  1. Press refresh, I get redirected to "Not Found" - this is the correct result.

Case 2: Session.get("redirectAfterLogin") is undefined

  1. Logout of the app.
  2. Enter in console Session.set("redirectAfterLogin");
  3. Login
  4. Observe the following output in console:
    • Accounts.onLogin()
    • no redirection, go root

But I am still on the "Unauthorised" Layout, with "welcome" template".

  1. Press refresh, I get redirected to "/" - this is the correct result.

What exactly is hindering the logic here? Please help!

user2587676
  • 117
  • 1
  • 11

2 Answers2

0

I had this problem when trying to redirect after logout in an event:

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

    FlowRouter.go('signin');
},

My quick solution was to add timeout for calling router:

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

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

You might want to increase the timeout.

Paweł C
  • 21
  • 4
0

This was the reason for my problem, the roles was not subscribed properly and thus I was stuck on the unauthorised layout. I hope this helps!

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();
  }
});
user2587676
  • 117
  • 1
  • 11