Hans
Hans

Reputation: 2910

FlowRouter, how to get data when routing depends on subscription

I'm writing a licence validation part for my application and want to redirect the user to a renewal page if and only if their licence has expired.

I am using FlowRouter and Blaze.

All my authenticated routes are in a group:

let authenticated = FlowRouter.group({
  triggersEnter: [checkAuthenticated, checkSubscription]
});

I then check if the subscription is valid like so:

const checkSubscription = function(context){
  let path = FlowRouter.current().path;
  if (!Meteor.userId()){
    return;
  }
  const sub = new Subscription();
  if (sub.isInvalid() && path !=="/manage-practice/subscription"){
    FlowRouter.go("/manage-practice/subscription");
  }
};

My class subscription uses a collection that I can only load once a user has logged in. My problem is that the router usually triggers this redirection before this data has been loaded.

Is there a best practice approach to solve this?

Upvotes: 0

Views: 575

Answers (1)

David
David

Reputation: 46

Redirect with Triggers

I'm not sure about this being 'best practice' but one approach is to use the Flow Router redirect functionality on your login event.

You can see examples at: https://atmospherejs.com/kadira/flow-router#redirecting-with-triggers and https://github.com/meteor-useraccounts/flow-routing.

The initial login path (using Accounts.onLogin();) could be to a generic 'loading...' template while you evaluate the user's collection. On a callback you can then use the custom redirect function to either redirect to the requested page in your app, or redirect the user to your '/manage-practice/subscription' path.

FlowRouter.wait()

I have to confess I wasn't previously familiar with this second option, but I've just come across FlowRouter.wait(). This can be useful to delay the default routing process until some other evaluation is complete. I suspect this might only be relevant if a user logs directly into a page within your authenticated routing group.

Documentation: https://atmospherejs.com/kadira/flow-router#flowrouter-wait-and-flowrouter-initialize

Upvotes: 0

Related Questions