Redirect to different routes depending on user upon login

I have a basic login system and upon login I’d like to redirect the user to their dashboard. There a 2 different types of dashboards in my application, regular users use one, and accounts of a specific type use the 2nd dashboard.

I am thinking that one way to handle this would be, upon login or account creation, do another lookup on the Meteor.users collection in the callback to check the newly logged in user’s account type. I could then route to the correct dashboard depending on those results.

Is there anything wrong with this implementation? Is there a better way? Having two sequentially calls to the server when logging in will add some time to the login process.

Why would you have two sequential calls? Just one. Upon login in the callback or using Tracker to watch Meteor.userId() you redirect. The data should already be pushed. Maybe you can customize your subscription for users to include the data you need so it’s pushed by default when a user logs in

Before the user logs in Meteor.user() will undefined. How would I watch this value so that when it populates I can redirect as needed?

Use Tracker as it’s reactive

I’m using React and react-meteor-data which incorporates Meteor’s Tracker system. Using createContainer, I can declare a prop like this.props.currentUser to represent Meteor.user(). However, I’m unsure on how the internals of createContainer work.

If I have something like:

Meteor.loginWithPass(options, function(err) {
    if (this.props.currentUser) {
        //check field on user document
        //route as needed
    }
})

I am unsure on whether this would cause some kind of race condition, as by the time the callback executes, I am not sure whether the container component created by createContainer() will have populate this.props.currentUser yet. If it hasn’t, I doubt Meteor.loginWithPass() will re-execute.

I am not an expert with React for Meteor, but I doubt it’s any different. You have two ways:

  1. In the callback after login you force the router to another page
  2. Use Tracker (Tracker.autorun) to monitor Meteor.userId() and redirect and it’s not null.

Don’t route, just handle it within a Layout component

I went ahead and tried the following. This redirects to the correct route when the user logs in, however, Tracker will still be “tracking” even once I’ve left my login component. For example, once I get to the dashboard, if I change the account type on the user document, this code will still run and a redirect will occur.

componentWillMount() {
    Tracker.autorun(() => {
      if (Meteor.user()) {
				
	if (Meteor.user().profile.isSecondaryAccount) {
	    browserHistory.push('/secondary/dashboard')
	} else {
            browserHistory.push('/normal/dashboard')
	}

      } else if(!Meteor.user()) {
           console.log('No user is logged in')
      }
    })
  }

What are you suggesting? Could you provide an example?

My application has a main content area where components are switched out via routes and this.props.children rendered in a top level <App /> component. It sounds like, with your suggestion, I’d need to restructure the app.

See here https://kadira.io/academy/meteor-routing-guide/content/implementing-auth-logic-and-permissions

Watch Meteor.userId(), store prior state and compare against it to identify new login