kadirahq / flow-router

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

How to call Meteor.user() in a trigger? #452

Closed trusktr closed 8 years ago

trusktr commented 8 years ago

I've got a trigger that needs the current user's data:

triggersEnter: [function() {
    const user = await Meteor.user() // race condition. Sometimes Meteor.user() isn't ready, even if the user is logged in.
    // ...
}]

Allowing async code would make it easier to solve this problem.

trusktr commented 8 years ago

For now, I'm doing this:

async function gotoFirstOrg(context, redirect, stop) {
    const user = await Users.current() // wait for async result
    const orgs = user.organizations().fetch()
    const lastOrg = orgs[0].slug

    FlowRouter.go(
        'organization',
        {organization: lastOrg},
        {}
    )
}

FlowRouter.route('/', {
    name: 'home',
    triggersEnter: [
        checkLogin,
        gotoFirstOrg,
    ],
    action: function (params) {}
})

gotoFirstOrg is the last trigger function, and the action is empty, so even though it is currently impossible to prevent the action with redirect or stop, it doesn't really matter in this case.

Async would be nice!

arunoda commented 8 years ago

As I said earlier, you need to get do this on the client side. Implement the redirect logic in the client side, if you need to get the userInfo for the redirect.

Just show a message to the user saying waiting, then do the redirect.