Best practice for app and landing page on the same route

I’d like to have both the root address “/” ro redirect to both the landing page if no user is signed in; and the actual if the user is logged in.

What’s the best way of doing so?

I tried doing it n a route level:

FlowRouter.route('/', {
	name: 'home',
	action: function() {
		if (!Meteor.user()) {
			// render landing blaze layout
		} else {
			// render app blaze layout
		}
	}
});

But it doesn’t seem very reliable, sometimes I get routed to the landing page even if the user is logged in.

If I do it on a template level:

<template name="entryPage">
	{{#if currentUser}}
		{{>app}}
	{{else}}
		{{>landing}}
	{{/if}}
</template>

I see the landing page flashing briefly before the app template is loaded.

Any advice?

Actually I always used a different approach which works pretty well for me.

FlowRouter.route('/', {
	name: 'home',
	action: function() {
			// render landing blaze layout
	}
});
FlowRouter.route('/app', {
    name: 'home',
    action: function() {
            // render landing app layout
    }
});

and then in template onCreate() i check for Meteor.user() and redirect to the app…

I think there is no “best practice”… just go with a solution which works for you - i would definitely prefer your first and my solution over your second, because I think it’s never really good to render two completely different templates on the same route out of one template.

1 Like

The best practice is not doing auth logic amd decisions on the router but doing it on your templates

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

4 Likes