kadirahq / flow-router

Carefully Designed Client Side Router for Meteor
MIT License
1.09k stars 193 forks source link

triggersEnter not run on FlowRouter.notFound #719

Open davesierra opened 7 years ago

davesierra commented 7 years ago

Setting up routes, I generally use an auth function to make sure the user is logged in, if not redirect to '/' so they can login. However using FlowRouter.notFound does not trigger as expected:

function ifAuth(context, redirect) {
    console.log('ifAuth: check for user:', Meteor.userId());
    if (!Meteor.userId()) {
        console.log('No Meteor user, redirect to /');
        redirect('/');
    }
}

FlowRouter.notFound = {
    name: 'not-found',
    triggersEnter: [ifAuth],
    action: function() {
                console.log('page not found?')
        mount(MainLayoutContainer, {
            children: <NotFoundPage />
        });
    }
};

If I go to a route like '/asdf' it will log 'page not found' but not 'ifAutho: check for user:'

What I have to do instead is set a catch-all (like in most other routers) in order to run the ifAuth on a route not found:

FlowRouter.route('/*', {
    name: 'undefined',
    triggersEnter: [ifAuth],
    action: function(params, queryParams) {
        console.log('not a valid route')
    }
});